Home > database >  How to update the showSnackBar in my code in Flutter
How to update the showSnackBar in my code in Flutter

Time:01-23

I am defining _scaffoldKey and then using it a few times further on in the code. How should I change to update the showSnackBar?

    final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

...

      if (result.savedOK!) {
        Navigator.pushReplacementNamed(context, MyShifts.id);
      } else {
        //show a snackbar
        _scaffoldKey.currentState!.showSnackBar(SnackBar(
          content: Text(
            _errMessage!,
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          ),
          backgroundColor: Colors.red,
          duration: new Duration(seconds: 3),
        ));
      }

CodePudding user response:

For show snackbar once try below code

///For show snackbar..
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
     content: Text(
     _errMessage!,
     style: TextStyle(fontSize: 20),
     textAlign: TextAlign.center,
     ),
     backgroundColor: Colors.red,
     duration: new Duration(seconds: 3),
     ));

I Hope these things are solve your issue.

  • Related