'showSnackBar' is deprecated and shouldn't be used

Viewed 26392

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"))
                      );
                    }
                  },
3 Answers

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")));

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.

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.

Related