Sub topic 6
Cache static subtrees with the builder child parameter
Before you start
Assume you know basic Dart classes, StatelessWidget, StatefulWidget, setState, and the build method. You should also be able to run a Flutter app on a device or emulator.
A builder widget calls a function to create part of its interface. Examples include ValueListenableBuilder, AnimatedBuilder, and StreamBuilder. When the value changes, the builder function runs again. If the builder contains a large static subtree, that subtree can be unnecessarily recreated or updated.
The child parameter provides a place for that static subtree. Flutter builds the child once when the parent builds, then passes the same widget instance to every later builder call.
The basic pattern
ValueListenableBuilder listens to a ValueListenable, such as ValueNotifier. Its builder receives the current value and an optional child:
ValueListenableBuilder<int>(
valueListenable: likes,
builder: (context, value, child) {
return Row(
children: [
Text('$value'),
child!,
],
);
},
child: const Icon(Icons.favorite),
)When likes changes, the builder runs again. The Text receives the new value. The icon comes from the child parameter and is reused.
The child must not depend on the value passed to the builder. Put only content there that can remain the same while the notifier changes.
A complete example
This app updates the number while keeping the static help card outside the builder’s changing work:
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: LikePage());
}
}
class LikePage extends StatefulWidget {
const LikePage({super.key});
@override
State<LikePage> createState() => _LikePageState();
}
class _LikePageState extends State<LikePage> {
late final ValueNotifier<int> _likes;
@override
void initState() {
super.initState();
_likes = ValueNotifier<int>(0);
}
@override
void dispose() {
_likes.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Likes')),
body: Center(
child: ValueListenableBuilder<int>(
valueListenable: _likes,
builder: (context, value, child) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Likes: $value'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => _likes.value++,
child: const Text('Like'),
),
const SizedBox(height: 16),
child!,
],
);
},
child: const HelpCard(),
),
),
);
}
}
class HelpCard extends StatelessWidget {
const HelpCard({super.key});
@override
Widget build(BuildContext context) {
return const Card(
child: Padding(
padding: EdgeInsets.all(24),
child: Text('Tap Like to update the counter.'),
),
);
}
}ValueListenableBuilder rebuilds its builder result when _likes.value changes. The HelpCard widget is supplied through child, so the builder does not create a new help-card widget for each notification.
The optimization is most useful when the child contains a complex subtree, such as an image, a menu, or several nested layout widgets. It does not make expensive work disappear if that work happens inside the builder itself.
| Arrangement | What changes when the value changes |
|---|---|
| Build everything inside `builder` | The builder creates all returned widgets again |
| Pass static content through `child` | The builder creates the changing part and reuses the child instance |
How to decide what belongs in `child`
Ask whether a widget needs the builder’s changing value.
- Put a counter, label, progress value, or color that depends on the value inside
builder. - Put a constant icon, decorative card, or static instructions in
child. - If a subtree needs both static and changing content, split it into a static child and a small dynamic widget.
- If the subtree is tiny, leave it inside the builder. Readability can matter more than a small allocation reduction.
Common mistakes
Do not create the supposedly static child inside the builder and expect the child optimization:
builder: (context, value, child) {
final card = const HelpCard();
return Column(children: [Text('$value'), card]);
},This code still constructs the card expression each time the builder runs. Pass it through the child argument instead.
Also remember that inherited dependencies can rebuild a child independently. For example, a child that reads changing MediaQuery data may rebuild when that data changes. The child parameter limits rebuilds caused by the listenable; it does not override every other dependency.
Exercise
In the example, where should a progress indicator that displays the current notifier value be placed?
Show answer
Inside the builder, because it depends on the changing value. A static legend or help card can be passed through child.