A Quick Look at BuildContext

 ・ 2 min

photo by Wolfgang Hasselmann on Unsplash

In Flutter, every widget has a build method. The build method always takes a context variable of type BuildContext as its parameter.
When you use context during the build phase, the widget that has the build method to implement can obtain information from parent widgets or widgets further up the tree through context.

ex) Provider.of(context), Theme.of(context), Scaffold.of(context), Navigator.of(context), MediaQuery.of(context) etc.

To find the widget you're looking for, it uses a Widget tree structure, traversing up from the current widget's position through its parents to search for the target widget.

Once the build function finishes executing and returns, that widget gets registered in the context, and from that point on, other widgets can find it.

image

The order Flutter processes widgets to draw them on screen
Widget -> Element -> RenderObject -> Canvas

If a class has the word Widget in it or inherits from an object with that name, it's implemented in the widget tree.
If a class has the word Element in it or inherits from an object with that name, it's implemented in the element tree.
If a class has the word RenderObject in it or inherits from an object with that name, it's implemented in the render tree.

Cases when the build method is called

  • After initState is called
  • After didUpdateWidget is called
  • After setState is called
  • After the State object has changed
  • After the State object is deactivated and reinserted at a different position in the tree

This link will be helpful for understanding Flutter animation principles: Turing Apple & Flutter Seoul


To be of use to the world is the only way to be happy.

— Hans Christian Andersen


Other posts
Flutter Feature Development Order 커버 이미지
 ・ 6 min

Flutter Feature Development Order

Accessing localhost from iPhone 커버 이미지
 ・ 2 min

Accessing localhost from iPhone

Understanding Flutter Rendering Errors 커버 이미지
 ・ 9 min

Understanding Flutter Rendering Errors