Sub topic 3
The matching rule: `runtimeType` and `key`
Assume you know basic Dart syntax, StatelessWidget, StatefulWidget, setState, and common widgets such as Column and ListView.
Flutter builds a new widget tree when state changes. It does not throw away the entire previous tree each time. Instead, it compares the new widgets with the existing Element tree. An Element is Flutter's long-lived record for a widget in a particular place in the tree.
This comparison decides whether Flutter can reuse an existing State object. The central rule is:
> A new widget can update an existing Element when both widgets have the same runtimeType and equal keys.
If either value differs, Flutter usually removes the old Element and creates a new one. Any State stored by that Element is then discarded.
The three trees
Flutter uses three related trees:
- The Widget tree contains immutable configuration objects.
- The Element tree connects widgets to the active application and persists between builds.
- The RenderObject tree handles layout and painting for widgets that draw on screen.
A State object belongs to the Element created for its StatefulWidget. Rebuilding normally creates new widget objects, but Flutter can keep the old Element and its State when the matching rule succeeds.
Matching widgets in the same position
Without keys, Flutter first compares children by position under their parent. Consider this simplified tree:
Column
├── CounterTile(label: A)
├── CounterTile(label: B)
└── CounterTile(label: C)After reversing the data, the new tree is:
Column
├── CounterTile(label: C)
├── CounterTile(label: B)
└── CounterTile(label: A)At the first position, both old and new widgets have the same runtimeType: CounterTile. Neither widget has a key, so both keys are null and therefore equal. Flutter reuses the first Element and its State. The label changes from A to C, but the counter State remains at that position.
The same happens at the third position. State follows the position, not the data item.
class CounterTile extends StatefulWidget {
const CounterTile({super.key, required this.label});
final String label;
@override
State<CounterTile> createState() => _CounterTileState();
}
class _CounterTileState extends State<CounterTile> {
int _count = 0;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text('${widget.label}: $_count'),
onTap: () => setState(() => _count++),
);
}
}The label belongs to the new widget configuration. The _count field belongs to the reused State object. These are separate objects, so changing the label does not reset the count.
Why both checks matter
The type check prevents Flutter from reusing an Element for an incompatible widget class. For example, a Text at a position cannot be updated as a CounterTile.
The key check distinguishes multiple widgets with the same type. A key is an identity attached to a widget. When keyed children are updated under the same parent, Flutter can use those identities to match an item with its previous Element instead of relying only on position.
Keys do not store State themselves. They help Flutter find the Element that already owns the State.
| Comparison | Result |
|---|---|
| Same type, same key | Reuse the Element and State |
| Different type | Replace the Element and State |
| Same type, different key | Replace or rematch to another keyed Element |
| Same type, both keys null | Match by position |
Exercise
Exercise
A CounterTile without a key moves from the first position to the third position after a list is reversed. Does its State follow the tile?
Show answer
No. With no keys, Flutter matches same-typed children by position, so the State stays with the Element's original position.