In my app, it is possible (and intended!) that multiple SnackBar elements are created. These SnackBar elements are queued and displayed one after another which is all well and fine. However, if I go to a different screen and then go back (so push and pop a route) all SnackBar elements are still there.
So I would like to remove all SnackBars upon pushing a route.
I tried counting my SnackBar elements and calling scaffoldKey.currentState.removeCurrentSnackBar() that many times in the following locations:
before
Navigator.push()which works, however, since I have multiple points where I push something I would like to use some callback.in
didPushRoute()fromWidgetsBindingObserver, however, even after registering the observer, this method is never called. (ChangeAppLifecycleState()is called so the registering seems to be not completely wrong?)in
dispose(). This actually produces my intended behavior but throws exceptions fromAnimatedBuilder(Exception text: "setState() or markNeedsBuild() called during build."), which I would like to avoid, even if the animation library seems to catch those.
If everything fails, I suppose I could just wrap Navigator.push() in a helper function that also clears the SnackBar queue but I hoped that there would be a more elegant way.