Last Flutter SnackBar is shown again each time I push a new route

705

Solution 1

The issue seemed to be related to the fact thatI was using SnackBars from two different libraries, one of them was ez_flutter the other was fluter material

Solution 2

You can use like this,

_scaffoldKey.currentState.removeCurrentSnackBar();**<--this close previous already open snackbar-->**
_scaffoldKey.currentState.showSnackBar(  **<--this open new same time-->**
   SnackBar(
     content: Text('Processing Data'),
   ),
 );

_scaffoldKey ;

Create key

Using key

Solution 3

To hide snackbar :-

Scaffold.of(context).hideCurrentSnackBar();

or if u define global key then :-

_scaffoldKey.currentState.removeCurrentSnackBar();
Share:
705
osantos
Author by

osantos

Updated on December 22, 2022

Comments

  • osantos
    osantos over 1 year

    I am new with Flutter development, I am developing a simple app that displays SnackBars to inform the user on the progress of the communication with the server. Everything seems to work correctly but after the snack bars have been displayed, when I Push a new route, the latest snackbar re-appears without me calling it. Once a SnackBar has been displayed and disappears, is there something that must be done to clean things up and avoid the snackbar from appearing again?

    • moatist
      moatist almost 4 years
      Can you post some code?
    • reverie_ss
      reverie_ss almost 3 years
      did u find a solution?
  • osantos
    osantos almost 4 years
    I already read about these possibility but the issue here is that the snackbar is already hidden or not visible when I navigate to the new screen and it simply shows again. When do you suggest I execute this method in order to avoid it from displaying when pushing a new route? Shouldn't SnackBars be disposed off once they have been shown? are they? If so, how come they show again when pushing a new route? I have tried executing Scaffold.of(context).hideCurrentSnackBar() before pushing the new route and still has no effect.