Sub topic 1
Widgets, Elements and the tree Flutter keeps between frames
Prerequisites
This chapter assumes basic Dart syntax, simple StatelessWidget and StatefulWidget classes, setState, and common layout widgets such as Column, ListView, and ListTile.
Three related trees
Flutter uses several trees to turn widget descriptions into pixels. This chapter focuses on the first three layers:
| Object | Main role | Typical lifetime |
|---|---|---|
| Widget | Immutable description of part of the UI | Usually one build |
| Element | Persistent position connecting a widget to the tree | Across many builds |
| State | Mutable data owned by a stateful element | While its element is reused |
A Widget is an immutable configuration object. For example, Text('Hello') describes text that Flutter should display. A build method creates new widget objects whenever its parent rebuilds.
An Element is Flutter's persistent record for a widget's position in the widget tree. It connects the widget configuration to its parent and children. It also provides the BuildContext used by many Flutter APIs.
A State object belongs to a StatefulWidget through its element. It stores mutable values such as a counter, a selected item, or text entered into a form.
What happens during a rebuild
Calling setState does not directly redraw one line of code. It marks the associated element as needing a build. Flutter later calls that widget's build method and receives a new subtree of widget objects.
Flutter then compares the new subtree with the existing element subtree. When a new widget matches the old widget in the same parent-child position, Flutter updates the existing element instead of replacing it.
For a StatefulWidget, this normally means that Flutter keeps the existing State object. The widget configuration changes, but the state survives.
This distinction explains why the following pattern works:
_countis stored in aStateobject.- A tap calls
setState. - The widget rebuilds and displays the new count.
- Flutter reuses the same element and state.
The new widget object is not the same Dart object created during the previous build. Reuse depends on Flutter's matching rules, not object identity.
The basic matching rule
For children under the same parent, Flutter first considers the widget's runtime type and key. A key is an optional identity label that helps Flutter decide which old widget corresponds to which new widget.
When the runtime type and key match, Flutter can update the existing element with the new widget. When they do not match, Flutter usually removes the old element and creates a new one. Creating a new element for a stateful widget also creates a new State object.
Without keys, position matters. The first new child is compared with the first old child, the second with the second, and so on. This is efficient for a fixed list, but it can produce surprising results when children move.
A practical prediction
Suppose a Column contains three stateful counter tiles. The first tile has count 4, and the second has count 1. If the list is rebuilt with the same children in the same order, both counts remain attached to their elements.
If the labels are reversed without giving the tiles stable keys, Flutter still compares the first new tile with the first old tile. The counter at position one remains at position one, even though its label changed. The state can therefore appear to belong to the wrong item.
Keys provide a way to describe item identity rather than relying only on position. The next chapter applies that rule to dynamic collections.
Exercise
Why can a StatefulWidget receive a new widget object while keeping the same State object?
Show answer
Flutter can update the existing Element when the new widget matches the old widget by runtime type and key in the relevant position. The Element continues to own the existing State object.