Understanding BuildContext

Gonçalo Palma
Flutter Community
Published in
6 min readApr 11, 2020

--

Photo by Tyler Lastovich on Unsplash

In Material Design, when we want to give some subtle feedback to the user, we may be inclined to use a SnackBar:

If we are using a Scaffold in our screens, we can easily show it by using the showSnackBar method with a button in the body:

But if we click on the button, no SnackBar is displayed, and we get the following error:

We now have two possible solutions:

  • Don’t read the error message and try to fix it any way;
  • Read the error message and try to fix it and understand why this happens;

Following the first route, we may be inclined to access the showSnackbarmethod, which resides in the ScaffoldState, with a GlobalKey:

--

--

Gonçalo Palma
Flutter Community

Author, Flutter Lead Developer @Pvotal Tech. Organizer, Google Developer Expert for Flutter and Dart, Lead Flutter Developer. 🌍 https://gpalma.pt/