Flutter – What does “context” of “Widget build(BuildContext context)” mean in Flutter

dartflutter

TL;DR:
Is it the second parameter of build method or, name declaration of the BuildContext parameter in a conventional way?

I'm learning the basic syntax of build method in Flutter by breaking down each element of "Widget build(BuildContext context)".

My understanding so far is below:

  • @override: annotation
  • Widget build() {}: build method returns a widget
  • BuildContext: a parameter/argument that contains information about the location in the tree at which this widget is being built

However, it's not been clear what the last word "context" exactly means even after I read related SO questions and official documents.

What I made sure so far is that the name "context" is changeable to whatever you want, meaning it doesn't have to be the same name as "context".

Best Answer

From the docs, BuildContext is:

A handle to the location of a widget in the widget tree.

context is a BuildContext instance which gets passed to the builder of a widget in order to let it know where it is inside the Widget Tree of your app.

One of the common uses is passing it to the of method when using an Inherited Widget.

Calling Something.of(context), for example, returns the Something relative to the closest widget in the tree that can provide you that Something.

You can read more about BuildContext here in the docs.

Related Topic