57

Trying to figure out this flutter problem. The below code has the showSnackbar as deprecated, and am trying to figure out with the fix is. The second code is my attempt to fix the problem. A new problem comes up with "The getter 'ScaffoldMessenger' isn't defined for the type 'ScaffoldState'.". The error tells me to import material.dart file but it is already imported.

Any help is appreciated.

              Padding(
                padding: const EdgeInsets.all(10.0),
                child: GestureDetector(
                  onTap: ()async{
                    if(!await authProvider.signIn()){
                      _key.currentState.showSnackBar(
                        SnackBar(content: Text("Login failed"))
                      );
                    }
                  },

              Padding(
                padding: const EdgeInsets.all(10.0),
                child: GestureDetector(
                  onTap: ()async{
                    if(!await authProvider.signIn()){
                      _key.currentState.ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(content: Text("Login failed"))
                      );
                    }
                  },

6 Answers 6

97

Based on the documentation here, it looks like the new ScaffoldMessenger handles all SnackBars below it. If you don't have multiple ScaffoldMessengers, you should just be able to call:

ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Login failed")));
3
  • Hi Joe, that is exactly what I have with the error message. Jan 26, 2021 at 18:14
  • Have you tried without referenceing _key.currentState before it? I'd try just with the ScaffoldMessenger Jan 26, 2021 at 18:17
  • 1
    worked for me too thanks. is that means no more need for globalKey<ScaffoldState>, am I right? May 10, 2021 at 23:58
20

This is the new way to add snackBars to the scaffold.

ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Incremented"), duration: Duration(milliseconds: 300), ), );

Using _key.currentState.ShowSnackBar(snackbar) Is deprecated now.

14

According to the official documentation:

'showSnackBar' is deprecated and shouldn't be used. Use ScaffoldMessenger.showSnackBar. This feature was deprecated after v1.23.0-14.0.pre..

New Way of displaying a snackbar:

ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Enter somethong here to display on snackbar")));

Old Method (using scaffold key):

_scaffoldkey.currentState.showSnackBar(snackbar);

This no longer can be used, avoid it and use the new way instead.

2

A simple answer to this is to create a global key in your stateful widget class Like so

...
final globalScaffoldKey = GlobalKey<ScaffoldMessengerState>();
...

add the variable to your Scaffold

...
 @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        key: globalScaffoldKey,
...

and you can access it like

...
globalScaffoldKey.currentState!.showSnackBar();
...
1

Using GlobalKey of type ScaffoldMessengerState

Create a key and assign it to the MaterialApp

final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

MaterialApp(
  scaffoldMessengerKey: scaffoldMessengerKey,
  child: ...
)

Displaying a Snackbar

scaffoldMessengerKey.currentState!.showSnackBar(
   SnackBar(content: Text("File Uploaded"))
);
0
1

Yes showSnackBar is deprecated seens 2.0.0 Stable Flutter SDK relese

You can use ScaffoldMessenger.of(context) widget to meke it work

Eg.

ScaffoldMessenger.of(scaffoldState.currentContext)
   ..removeCurrentSnackBar()
   ..showSnackBar(
      SnackBar(
        content: Text(value),
        backgroundColor: Colors.black,
        duration: Duration(seconds: 2),
     ),
   );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.