Member-only story

FLUTTER INTERVIEW QUESTIONS

What exactly is that BuildContext you keep seeing in your build methods?

Jelena Lecic
2 min readJan 2, 2020

--

As you probably already know, Widgets are referenced from the Element tree nodes. For every Widget that you see on your screen, there’s a corresponding Element that keeps informations about Widget’s relations, its state etc.

…and BuildContext is actually an Element!

While creating Widgets, you’re provided with the context parameter.

Widget build(BuildContext context) {
return ...;
}

But, whose BuildContext is that? From the Widget that’s being created? Well… no.

Let’s see…
Have you tried to display a SnackBar inside a Scaffold Widget like this?

Scaffold.of(context) method looks UP(from that context node) the Element tree and returns the nearest Scaffold’s State. The BuildContext from build method(appContext) is from the MaterialApp’s parent’s Element(MyApp).

--

--

No responses yet