'Flutter snackbar dismiss on SnackBarAction onPressed

I want to dismiss SnackBar on SnackBarAction's onPressed method. I tried with Navigator.of(context).pop(); but SnackBar is not dismissing my screen get black instead.

Here is code:

 void showInSnackBar(String value) {
homeScaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value),
  action: SnackBarAction(
    label: 'Dissmiss',
    textColor: Colors.yellow,
    onPressed: () {
    //  Navigator.of(context).pop();
    },
  ),));
}


Solution 1:[1]

Try using hideCurrentSnackBar method

onPressed: () {
    homeScaffoldKey.currentState.hideCurrentSnackBar();
},

More info here: https://docs.flutter.io/flutter/material/ScaffoldState/hideCurrentSnackBar.html

Solution 2:[2]

You can also use,

Scaffold.of(context).hideCurrentSnackBar();

Be careful when you use context, use the correct context.

NOTE

In the new Flutter Version, this method is deprecated. Therefore use

ScaffoldMessenger.of(context).hideCurrentSnackBar();

Solution 3:[3]

ScaffoldMessenger.of(context).hideCurrentSnackBar();

Solution 4:[4]

If you want to replace snackbar that show only one time,

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  final snackBar = SnackBar(content: Text("Hello, world"));

And also,

@override
Widget build(BuildContext context) {
return Scaffold(
  key: _scaffoldKey,

And also,

onPressed: () {
    _scaffoldKey.currentState.removeCurrentSnackBar();
    _scaffoldKey.currentState.showSnackBar(snackBar);
     }

Solution 5:[5]

Define your SnackBar:

var snackBar = SnackBar(content: Text('Hello World'));

To show it:

ScaffoldMessenger.of(context).showSnackBar(snackBar);

To hide it:

ScaffoldMessenger.of(context).hideCurrentSnackBar();

To hide the last one and show a new one:

ScaffoldMessenger.of(context) 
  ..hideCurrentSnackBar()
  ..showSnackBar(snackBar);

Solution 6:[6]

To clear the previous snackbars & show only the new one, use removeCurrentSnackBar method rather than hideCurrentSnackBar as it does not clear the stack. So the code will be

ScaffoldMessenger.of(context) 
  ..removeCurrentSnackBar()
  ..showSnackBar(snackBar);

Solution 7:[7]

Scaffold.of(context).hideCurrentSnackBar(); Above method is used previously but,

ScaffoldMessenger.of(context).hideCurrentSnackBar(); This is now recommended.

Solution 8:[8]

You can also show and dismiss a snackbar like this without any key

ScaffoldMessenger.of(context).showSnackBar(
  SnackBar(
    content: Text('Hello from snackbar!'),
    action: SnackBarAction(
      label: 'Dissmiss',
      textColor: Colors.yellow,
      onPressed: () {
        ScaffoldMessenger.of(context).hideCurrentSnackBar();
      },
    ),
  ),
);

Solution 9:[9]

All these answers will not work because you cannot reference the ScarfoldMessenger from a Snackbar. You'll have to save a reference to the snackbar and call it's "close" method. Like so void Function () close; var snackbar = ScaffoldMessenger.of(context).showsnackbar(Snackbar (content:Text(), action: SnackbarAction(onPressed:()=>close())) close = ()=> snackbar.close();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 diegoveloper
Solution 2
Solution 3 Rizwan Ansar
Solution 4 Hussnain Haidar
Solution 5 CopsOnRoad
Solution 6 Kamlendra Pandey
Solution 7 Rishu Roy
Solution 8 Maddu Swaroop
Solution 9 Seth Samuel