Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display SnackBar in Flutter

I want to display a simple SnackBar inside Flutter's stateful widget. My application creates new instance of MaterialApp with a stateful widget called MyHomePage.

I try to show the SnackBar in showSnackBar() method. But it fails with 'The method 'showSnackBar' was called on null'.

What's wrong with this code?

class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new MaterialApp(       title: 'Flutter',       theme: new ThemeData(         primarySwatch: Colors.blue,       ),       home: new MyHomePage(),     );   } }  class MyHomePage extends StatefulWidget {   MyHomePage({Key key}) : super(key: key);    @override   _MyHomePageState createState() => new _MyHomePageState(); }  class _MyHomePageState extends State<MyHomePage> {    final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();    @override   void initState() {     super.initState();     showInSnackBar("Some text");   }    @override   Widget build(BuildContext context) {     return new Padding(             key: _scaffoldKey,             padding: const EdgeInsets.all(16.0),             child: new Text("Simple Text")     );   }    void showInSnackBar(String value) {     _scaffoldKey.currentState.showSnackBar(new SnackBar(         content: new Text(value)     ));   } } 

SOLUTION:

class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new MaterialApp(         title: 'Flutter',         theme: new ThemeData(             primarySwatch: Colors.blue,         ),         home: new Scaffold(body: new MyHomePage()),       );   } }  class MyHomePage extends StatefulWidget {   MyHomePage({Key key}) : super(key: key);    @override   _MyHomePageState createState() => new _MyHomePageState(); }  class _MyHomePageState extends State<MyHomePage> {    @override   void initState() {     super.initState();   }    @override   Widget build(BuildContext context) {     showInSnackBar("Some text");     return new Padding(         padding: const EdgeInsets.all(16.0),         child: new Scaffold(           body: new Text("Simple Text")         )     );   }    void showInSnackBar(String value) {     Scaffold.of(context).showSnackBar(new SnackBar(         content: new Text(value)     ));   } } 
like image 207
kosiara - Bartosz Kosarzycki Avatar asked Nov 13 '16 23:11

kosiara - Bartosz Kosarzycki


People also ask

How do I display SnackBar Flutter?

The ElevatedButton is a child widget placed on the return Center . Then, we use the ScaffoldMessenger class to display the SnackBar. Clicking on the button will show the SnackBar with the following message: “Hi, I am a SnackBar!”

How do you show SnackBar in Flutter without scaffolding?

We are going to use the property scaffoldMessengerKey which allows us to show snackbars without needing context with just one GlobalKey. Also, no need to add 3rd party packages like Get. //main. dart import 'globals.

How do you use SnackBar animation in Flutter?

First, you have to create a SnackBar which can be done by calling the following constructor. The only required parameter is content which is the widget to be displayed inside the snackbar. In most cases, the widget is a Text widget, but you can also use other types of Flutter widget.


Video Answer


2 Answers

In my case i had code like this (in class state)

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();  void showInSnackBar(String value) {     _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value))); } 

but i didn't setup the key for scaffold. so when i add key: _scaffoldKey

 @override  Widget build(BuildContext context) {   return new Scaffold(     key: _scaffoldKey,     body: new SafeArea( 

snackbar start to work :)

like image 101
Jakub S. Avatar answered Sep 21 '22 17:09

Jakub S.


There's three problems. The first is that you don't have a Scaffold anywhere, and the Scaffold widget is the one that knows how to show snack bars. The second is that you have a key for getting a hold of the scaffold, but you've put it on a Padding instead (and Paddings don't have any knowledge of snack bars). The third is that you've used the key before the widget that it's associated with has had a chance to be initialised, since initState is called before build.

The simplest solution is to change the home line in your MyApp widget to:

home: new Scaffold(body: new MyHomePage()),

...and then remove all mention of _scaffoldKey and instead use Scaffold.of(context) where you currently have _scaffoldKey.currentState.

like image 25
Ian Hickson Avatar answered Sep 21 '22 17:09

Ian Hickson