Sub topic 4
Immutable Widgets and `const` Constructors
At the start of this chapter, we assume that you know basic Dart classes, constructors, final fields, named parameters, and null safety. You also need a Flutter project that can run a simple app.
What immutable means
An immutable object cannot change after it is created. Flutter widgets are immutable descriptions of user interface configuration. Their properties should describe what the widget needs to display, not hold changing data.
A widget usually stores its configuration in final fields:
import 'package:flutter/material.dart';
class Greeting extends StatelessWidget {
const Greeting({
super.key,
required this.name,
});
final String name;
@override
Widget build(BuildContext context) {
return Text('Hello, $name');
}
}The name field cannot be reassigned after the Greeting object is created. If the name changes, Flutter receives a new Greeting widget with a different value.
Immutability makes widgets easier to reason about. A widget can be rebuilt many times without changing internally. Flutter can compare the new configuration with the old configuration while keeping the corresponding state and elements in the widget tree.
StatefulWidget is also immutable. Its changing data belongs in the separate State object, not in the widget itself.
What a `const` constructor does
A const constructor allows Dart to create an object at compile time when every constructor argument is also a compile-time constant.
class Label extends StatelessWidget {
const Label({
super.key,
required this.text,
});
final String text;
@override
Widget build(BuildContext context) {
return Text(text);
}
}
class LabelPage extends StatelessWidget {
const LabelPage({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Label(text: 'Ready'),
);
}
}The const before Center applies to the entire constant subtree. The Label constructor also needs to be const for this to work.
Dart can reuse identical constant objects instead of creating new objects each time the code runs. Flutter can then recognize that a constant widget configuration has not changed and avoid unnecessary work in that part of the tree.
Use const when the widget and all of its arguments are known at compile time. Do not force it when a value depends on runtime data.
| Situation | Example | Use `const`? |
|---|---|---|
| Fixed text | `const Text('Welcome')` | Yes |
| Runtime text | `Text(user.name)` | No |
| Fixed padding | `const EdgeInsets.all(16)` | Yes |
| Value stored in changing state | `Text('Count: $_count')` | No |
Const and rebuilding
A parent can rebuild even when one of its children is constant. This is normal. Flutter still evaluates the parent's build method, but a constant child can be reused when its configuration remains identical.
Prefer small widgets with const constructors. This practice documents which configuration is fixed and can reduce object creation during rebuilds. It does not replace good widget structure or make expensive work in build harmless.
Common errors
- Declaring a widget field without
final. - Putting a counter or controller directly inside a widget.
- Adding
constwhile passing a runtime value. - Assuming
constprevents every descendant from rebuilding. - Forgetting to add a
constconstructor to a reusable widget.
Exercise
Why can Label(text: 'Ready') be used with const, but Text(user.name) usually cannot?
Show answer
'Ready' is a compile-time constant string, while user.name is read at runtime and may change.