0

I was trying to show a Snackbar after navigating to a new page with the Get Package. I passed the "snackbar-ingredients" as a parameter to the new widget and wanted to show it then. I got an error that indicates that the built of the widget didn't finish before I tried to show the snackbar. That obviously didn't work. I get why.. but I don't have a fitting solution.

Is there a way to wait for the build to finish before calling the snackbar?

Thanks for your help!

(PS: in another post someone said to refer to the scaffoldKey.. I am not sure how to do that in Get.snackbar())

  • 1
    Future.delayed(Duration(seconds:2),()=> Get.snackbar()).whenComplete(()=> Get.toNamed());. Try this if this helps. – Arbiter Chil Sep 16 '21 at 17:56
  • 2
    Take a look at `WidgetsBinding.instance.addPostFrameCallback` from this answer https://stackoverflow.com/a/51273797/2301224. This will run code you supply (such as showing a snackbar) after the widget build is complete. – Baker Sep 16 '21 at 19:17
  • WidgetsBinding.instance.addPostFrameCallback That's the one! You are a livesaver! Thanks! :) – Simon Lemcke Sep 16 '21 at 20:07

1 Answers1

0

@Arbiter Chill answer works for me. I tried this:

Future.delayed(const Duration(seconds: 1), ()=>Get.snackbar("Error", response.body,
        backgroundColor: AppColor.blueBackgroundColor, colorText: AppColor.white));
Abdul Rahman Shamair
  • 580
  • 1
  • 11
  • 22