Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 727 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'showSnackBar' is deprecated and shouldn't be used

#1
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"))
);
}
},

Reply

#2
Based on the [documentation here][1], 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")));


[1]:

[To see links please register here]

Reply

#3
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.




Reply

#4
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.
Reply

#5
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"))
);
Reply

#6
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),
),
);
```
Reply

#7
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();
...
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through