How to show a snackbar through a method in Flutter?

Posted by Andre Scott | Updated on

Want to show a snackbar through a method in Flutter? The problem is the a StatelessWidget doesn’t have a context member – it only has access to the context which is passed in it’s build method. In general, if you need access to a context and don’t have one you should consider a StatefulWidget. The State object, being persistent, always has access to context.

You also shouldn’t try to save the context object in a StatelessWidget, since the framework doesn’t guarantee the saved context is valid when your callback fires.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers