Sub topic 5
How an element is mounted, updated, and unmounted
This chapter assumes basic Dart, StatelessWidget, StatefulWidget, and widget composition with Row, Column, Padding, and Text.
An element is a long-lived object that connects a widget configuration to its place in the widget tree. Flutter changes the widget objects often, but it tries to keep matching elements. This lets state and other resources survive ordinary rebuilds.
The element lifecycle
An element commonly passes through these stages:
- Mount: Flutter creates the element, connects it to its parent, and builds its first child.
- Update: Flutter gives the existing element a new widget configuration.
- Deactivate: The element is removed from its current position temporarily.
- Activate: A deactivated element is placed back into the tree.
- Unmount: Flutter permanently removes the element.
A StatefulElement also owns a State object. Flutter calls initState once when that state is first mounted. It calls didUpdateWidget when the same element receives a new widget of the same type and key. Flutter calls dispose when the element is permanently unmounted.
Mounting and updating
When a parent first includes a child, Flutter creates the child's element. For a StatefulWidget, the element creates its State, calls initState, and then runs build.
During a rebuild, the parent compares its old child widget with the new child widget. If their runtime types and keys match, Flutter updates the existing element instead of creating another one.
import 'package:flutter/widgets.dart';
void main() => runApp(const Directionality(
textDirection: TextDirection.ltr,
child: CounterLabel(),
));
class CounterLabel extends StatefulWidget {
const CounterLabel({super.key});
@override
State<CounterLabel> createState() => _CounterLabelState();
}
class _CounterLabelState extends State<CounterLabel> {
@override
void initState() {
super.initState();
debugPrint('mounted');
}
@override
void didUpdateWidget(covariant CounterLabel oldWidget) {
super.didUpdateWidget(oldWidget);
debugPrint('widget configuration changed');
}
@override
void dispose() {
debugPrint('unmounted');
super.dispose();
}
@override
Widget build(BuildContext context) {
return const Text('Hello');
}
}The State object is not recreated for each build. This is why fields such as counters, controllers, and subscriptions can retain their values. Resources acquired in initState should be released in dispose.
Common lifecycle errors
- Starting a subscription in
buildcan create a new subscription on every rebuild. - Forgetting
super.initState()orsuper.dispose()can violate theStatelifecycle contract. - Calling
setStateafterdisposemeans asynchronous work outlived its widget. Cancel the work or checkmountedbefore updating. - Changing a widget's key can cause its old element and state to be replaced rather than updated.
Exercise
A widget rebuilds with the same runtime type and key. Is its State object recreated?
Show answer
No. Flutter updates the existing element and keeps its State object. It may call didUpdateWidget and then rebuild the widget's subtree.