Sub topic 4
Use const to reuse widget instances
Assume you know basic Dart classes, StatelessWidget, StatefulWidget, setState, and the build method. This chapter focuses on one question: what can Flutter avoid rebuilding when a parent builds again?
What `const` changes
In Dart, const creates a compile-time constant object. When the same constant expression appears again, Dart can reuse the same widget instance. Flutter uses that identity when it updates the widget tree.
A parent can rebuild because one piece of state changed. Its build method may return the same constant child as before. Flutter sees that the old and new widget objects are identical, so it can keep the existing child element without calling that child's build method again.
const does not stop the parent from rebuilding. It also does not make a widget's changing data update automatically. Use it for widgets whose constructor arguments are all constant.
| Widget code | What can happen on a parent rebuild |
|---|---|
| `const Text('Profile')` | The same widget instance can be reused |
| `Text('Likes: $_likes')` | A new widget is needed when `_likes` changes |
A const child in a changing screen
The following screen rebuilds after every tap. The header and footer remain constant, while the likes label receives a new value.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ProfileScreen(),
);
}
}
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
int _likes = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: Column(
children: [
const ExpensiveHeader(),
Text('Likes: $_likes'),
ElevatedButton(
onPressed: () => setState(() => _likes++),
child: const Text('Like'),
),
const ExpensiveFooter(),
],
),
);
}
}
class ExpensiveHeader extends StatelessWidget {
const ExpensiveHeader({super.key});
@override
Widget build(BuildContext context) {
return const Padding(
padding: EdgeInsets.all(24),
child: Text(
'Profile information',
style: TextStyle(fontSize: 24),
),
);
}
}
class ExpensiveFooter extends StatelessWidget {
const ExpensiveFooter({super.key});
@override
Widget build(BuildContext context) {
return const Padding(
padding: EdgeInsets.all(24),
child: Text('Account settings'),
);
}
}The const keyword on a constructor is required before you can create a widget with a constant expression. Add const to the constructor when every field can be initialized at construction time and never changes.
The const keyword at the call site is also useful. In this example, const ExpensiveHeader() and const ExpensiveFooter() produce reusable instances. The Text inside the button is constant as well.
How the update works
The framework compares a parent element's old and new child widgets. If they are the identical object, Flutter can skip the normal update for that child. If they are different objects of the same type and key, Flutter usually updates the existing element and may call build again.
This means const is most useful for stable subtrees such as:
- Static labels and icons.
- Fixed decorations and empty-state messages.
- Headers, footers, and navigation items with no changing inputs.
- Small extracted widgets that receive only constant arguments.
Common mistakes
Do not force const onto widgets that depend on runtime values:
Text('Likes: $_likes')This expression must be rebuilt when _likes changes. Making a changing value constant would display stale data or fail to compile.
Also remember that a const constructor does not make every use constant. This is constant:
const Icon(Icons.favorite)This is not constant when the color comes from a runtime value:
Icon(Icons.favorite, color: themeColor)When a widget has both fixed and changing parts, keep the fixed parts constant and extract them when that makes the rebuild boundary clearer.
Exercise
In the example, which widgets must be rebuilt or recreated when _likes changes, and which widgets can be reused?
Show answer
The Text('Likes: $_likes') widget needs new data, and the parent build method runs again. The constant header, footer, app bar title, and button label can reuse their constant widget instances.