user14893742
user14893742

Reputation:

Flutter - 'showSnackBar' is deprecated - how to update?

I have some outdated parts in my main.dart, i would like to update them, but need a little help because my knowledge is apparently insufficient, hope someone can help me :-)

in this part the snack bar is out of date, as you can see in the message.

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

This is the associated code of my main.dart

  Navigator.of(context, rootNavigator: true).pop('dialog');
  Magazin.scaffoldKey.currentState!.showSnackBar(SnackBar(
    content: Text('BEIM LADEN DER POST-DATEN IST EIN FEHLER AUFGETRETEN!'),
    duration: Duration(seconds: 5),
  ));

I found this therad here in the forum but it didn't really help me ... 'showSnackBar' is deprecated and shouldn't be used

Upvotes: 2

Views: 6886

Answers (5)

Eng Thanoon
Eng Thanoon

Reputation: 69

instead of line

   Magazin.scaffoldKey.currentState!.showSnackBar(SnackBar(

but this line

    ScaffoldMessenger.of(context).showSnackBar(SnackBar(

Upvotes: 1

Rasathurai Karan
Rasathurai Karan

Reputation: 801

If you're working with Flutter 2 or higher, you might see a deprecation warning regarding the hideCurrentSnackBar() and showSnackbar() methods.

You can get rid of them by using:

ScaffoldMessenger.of(context).hideCurrentSnackBar() ScaffoldMessenger.of(context).showSnackbar(...) instead of

Scaffold.of(context).hideCurrentSnackBar() Scaffold.of(context).showSnackbar(...)

Upvotes: 0

Andy S
Andy S

Reputation: 49

For me, it wasnt obvious that you needed wrap your old Scaffold(...) with a ScaffordMessager(...). As soon as I did this, migration was easy with the above.

New over-ridden build:

@override
  Widget build(BuildContext context) {
    return ScaffoldMessenger(
      key: scaffoldMessengerKey,
      child: Scaffold(
      appBar: AppBar(

Old snippet:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(

Hope this helps the next person!

Upvotes: 2

Amit Singh
Amit Singh

Reputation: 753

you can use Flushbar for this features in your app: flushbar

FlushbarHelper.createError(
       message: failure.map(
              cancelledByUser: (_) => 'Cancelled',
              serverError: (_) => 'Server error',
              emailAlreadyInUse: (_) => 'Email already in use',
              invalidEmailAndPasswordCombination: (_) =>
                  'Invalid email and password combination',
            ),
          ).show(context);

Upvotes: -1

Sina
Sina

Reputation: 168

you can use this code .if rest of your code work currently this code doesn't have any problem

Navigator.of(context, rootNavigator: true).pop('dialog');
ScaffoldMessenger.of(context).showSnackBar(
     SnackBar(
          content:Text('BEIM LADEN DER POST-DATEN IST EIN FEHLER AUFGETRETEN!'),
          duration: Duration(seconds: 2),
    ),
);

Upvotes: 8

Related Questions