I am trying to show a snack bar in a tabbed flutter app as follows:
var scaffoldKey = new GlobalKey<ScaffoldState>();
scaffoldKey.currentState
.showSnackBar(new SnackBar(
content: new Text("This is a message")
);
});
But I am getting the error:
NoSuchMethodError: The method 'showSnackBar' was called on null.
In the main.dart file I initialize the app as follows:
runApp(new MaterialApp(...));
And then the snackBar is being called in home.dart which has a tabbed interface:
Widget build (BuildContext context) => new Scaffold(…);
So I'm a bit confused on which scaffold to call the snackbar on, and how, and also why scaffoldKey.currentState
is null.
Best Answer
You need to pass Key to
Scaffold
Widget also.Then you can call -
scaffoldKey.currentState.showSnackBar