Reputation: 647
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"))
);
}
},
Upvotes: 58
Views: 53818
Reputation: 59
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();
...
Upvotes: 4
Reputation: 525
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),
),
);
Upvotes: 1
Reputation: 9764
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"))
);
Upvotes: 1
Reputation: 459
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.
Upvotes: 23
Reputation: 1214
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.
Upvotes: 14
Reputation: 17824
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")));
Upvotes: 102