Sub topic 7
Reconciliation: Reuse, Update, or Replace
Assume you know basic Dart and can write simple StatelessWidget and StatefulWidget classes. Reconciliation is Flutter’s process for comparing the old widget tree with a new one after a rebuild. It decides which existing elements can stay, which widgets must be updated, and which elements must be replaced.
The matching rule
Widgets are immutable descriptions. During a rebuild, a parent usually creates new widget objects. Flutter does not automatically create a new element for every new widget. Instead, it compares each new widget with the old widget at the same location.
An element can update its existing widget when both conditions are true:
- The widgets have the same runtime type.
- The widgets have equal keys.
Conceptually, Flutter uses this rule:
bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType &&
oldWidget.key == newWidget.key;
}When the rule succeeds, Flutter keeps the element and gives it the new widget. The element then updates its child. For a StatefulWidget, the associated State object also stays attached to that element.
When the rule fails, Flutter removes the old element from that position and mounts a new element for the new widget. A new State object is created if the replacement is stateful.
A simple update
Suppose a parent rebuilds from Text('Loading') to Text('Ready'). Both widgets have the same runtime type, Text, and normally have no keys. Flutter reuses the existing Text element and updates its widget configuration. The render object can then update the displayed paragraph.
Changing a widget’s constructor values does not, by itself, discard its element or state. Changing its type or key can.
import 'package:flutter/widgets.dart';
class Status extends StatelessWidget {
const Status({super.key, required this.ready});
final bool ready;
@override
Widget build(BuildContext context) {
return Text(ready ? 'Ready' : 'Loading');
}
}
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
bool _ready = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() => _ready = !_ready),
child: Status(ready: _ready),
);
}
}Tapping calls setState, causing Example to rebuild. The Status widget at the same position remains the same type, so its element is reused.
Why keys matter in lists
Without keys, siblings are commonly matched by position. If a list changes from [A, B] to [B, A], the first existing element may be updated with B, and the second with A. Any state stays at those positions rather than following the labels.
A ValueKey gives Flutter an identity for each item. Flutter can then match the item with the same key even after it moves.
Exercise
A stateful widget changes from Panel(key: ValueKey('old')) to Panel(key: ValueKey('new')) at the same position. Is its existing State object reused?
Show answer
No. The keys differ, so Flutter replaces the element and creates new state.