2

The newly created State object is associated with a BuildContext. This association is permanent: the State object will never change its BuildContext. However, the BuildContext itself can be moved around the tree along with its subtree.

what does this statement trying to say? i find it quite subtle. it is from the official documentation of flutter

2
  • Where did you find this? Can you include a link? Commented Jan 8, 2020 at 14:20
  • It comes from the State's lifecycle definition api.flutter.dev/flutter/widgets/State-class.html Commented Jan 8, 2020 at 14:51

1 Answer 1

4

There are a lot of core concepts here, first of all you need to understand how flutter render the widgets, ill try to make a summary.

At run time, flutter internally manage three trees in order to achieve the high performance: Widget tree, Element tree and RenderObject tree.

I'm not going to get deep into this since is complicated but basically each tree has different responsibilities:

  • Widget: describe the configuration for an Element. It handle Configuration.

  • Element: an instantiation of a Widget at a particular location in the tree. It manage Life cycle.

  • RenderObject: handles size, layout, etc. It handle render and painting aspects.

So, for every widget, Flutter builds a corresponding Element and build the Element Tree.

For Stateless widgets, the relation between widget and the corresponding element is trivial, but for Stateful widgets the underlying Element structure looks a little different. Those elements add a state object, which holds the mutable part of the configuration, a color for example.

The other thing that you should know is that BuildContext is actually a Element.

With that in mind, the meaning of this:

The newly created State object is associated with a BuildContext. This association is permanent: the State object will never change its BuildContext. However, the BuildContext itself can be moved around the tree along with its subtree.

Is trying to say that when you build a Stateful widget, flutter is going to build a BuildContext (an element that hold the widget position, among other properties) and that contexts will hold the mutable State object.

Then the buildContext (element) itself can change (moved on the tree for example), but the thing that never is going to happen is that changing the state object will change the BuildContext. And that's why you can change, for example, the widget color or any mutable property on State object, and it will never change the element position in the tree.

Is a really interesting topic but is not simple. I highly recommend you to check this video and this article that have a deep explanation into the this.

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Do you mean an Element can move around on the Element tree?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.