What I did :
context
and the setState()
of each page in static variables with these two lines getting called in all the build functions of my pages :@override Widget build(BuildContext context) { StaticClass.currentContext = context; StaticClass.currentSetState = this.setState; return ... ; }
fcm.configure( onMessage: (){ StaticClass.currentSetState((){ Navigator.pushNamed(StaticClass.currentContext, "/notifications"); }); });
What happened :
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══ ... setState() or markNeedsBuild() called during build. This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. ...
Explanations :
What I want :
OR
OR
Please Help I'm stuck here
Because we can't call setState() directly in the initState method, we can't call it from a function called by initState either. Unless this function is asynchronous and the setState call is made after the first await.
Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.
setState is a way to dynamically change the UI. We call it inside the State Object class of the StatefulWidget. Calling setState marks the corresponding Widget dirty .
You can call setState
after rendering is done by adding a post frame callback with addPostFrameCallback method. This will be called only once and after build process is done.
WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With