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

4,431

Solution 1

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

Solution 2

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!

Share:
4,431
Admin
Author by

Admin

Updated on December 06, 2022

Comments

  • Admin
    Admin over 1 year

    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