flutterdartflutter-widgetsnackbarscaffold

Having a red line showSnackBar and error in when running the program


I am running my flutter app on Android Studio (mac) and I get this error: enter image description here

How do I fix this? This is the original code:

Future showCopiedToClipboard(BuildContext context, String text) async {
  final lang = Language.of(context);
  await FlutterClipboard.copy(text);
  final content = lang.copiedToClipboard(text);
  if (Scaffold.maybeOf(context) != null)
    Scaffold.of(context).showSnackBar(SnackBar(
      content: content,
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
    ));

I tried to search for it on Google but got no correct answer that would help me solve this.


Solution

  • Use ScaffoldMessenger.of(context) instead of Scaffold.of(context)

    Like so:

     ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: content,
          backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        ));