Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'showSnackBar' is deprecated and shouldn't be used

Tags:

flutter

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"))
                      );
                    }
                  },
like image 859
scubadivingfool Avatar asked Jan 26 '21 17:01

scubadivingfool


People also ask

Is SnackBar deprecated?

showSnackBar shows a SnackBar at the bottom of the scaffold. This method should not be used, and will be deprecated in the near future..

How do I get rid of SnackBar flutter?

You can hide the current SnackBar before the end of its duration by using: ScaffoldMessenger. of(ctx). hideCurrentSnackBar();

How do you show SnackBar in flutter without scaffolding?

you probably have a parent widget that contains Scaffold as well. Create a scaffoldKey for that and pass it to your child widget that have to show the Snakcbar . Note that you can't use Sanckbar without Scaffold widget.

How do I change the SnackBar position in flutter?

You can do this by placing a container inside the snackbar. Since snackbar can take any widget and you can change its background color to transparent, you can use a container with custom padding and borders to give an illusion of positioning.


2 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")));
like image 51
Joe Muller Avatar answered Oct 09 '22 23:10

Joe Muller


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.

like image 16
jordy bayo Avatar answered Oct 09 '22 23:10

jordy bayo