Sub topic 10
Limiting Rebuilds with `select`
Assume you know Dart classes and generics, Flutter’s build method, and Riverpod providers, Notifier, and ref.watch. This chapter focuses on reducing widget rebuilds when a provider contains more data than a widget needs.
Why rebuilds matter
When a watched provider changes, Riverpod checks each listener. A widget that watches the complete value may rebuild even when the part it displays is unchanged.
For example, a profile might contain a name and an age. A name label does not need to rebuild when only the age changes. The select method lets a widget watch one part of a provider’s value.
Selecting one field
select receives the complete provider value and returns the piece that matters to the widget.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Profile {
const Profile({required this.name, required this.age});
final String name;
final int age;
}
class ProfileNotifier extends Notifier<Profile> {
@override
Profile build() => const Profile(name: 'Ada', age: 36);
void birthday() {
state = Profile(name: state.name, age: state.age + 1);
}
void rename(String name) {
state = Profile(name: name, age: state.age);
}
}
final profileProvider =
NotifierProvider<ProfileNotifier, Profile>(ProfileNotifier.new);
class NameLabel extends ConsumerWidget {
const NameLabel({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final name = ref.watch(profileProvider.select((profile) => profile.name));
return Text('Name: $name');
}
}
class ProfilePage extends ConsumerWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final age = ref.watch(profileProvider.select((profile) => profile.age));
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const NameLabel(),
Text('Age: $age'),
ElevatedButton(
onPressed: () =>
ref.read(profileProvider.notifier).birthday(),
child: const Text('Birthday'),
),
ElevatedButton(
onPressed: () =>
ref.read(profileProvider.notifier).rename('Grace'),
child: const Text('Rename'),
),
],
),
),
);
}
}
void main() {
runApp(
const ProviderScope(
child: MaterialApp(home: ProfilePage()),
),
);
}When birthday creates a new Profile, the name remains "Ada". NameLabel therefore does not rebuild. When rename changes the name, NameLabel rebuilds with the new value.
select limits consumer rebuilds. It does not necessarily stop the provider or its other listeners from updating.
Choosing a selected value
The selected result should be stable and represent exactly what the widget needs.
| Selection | Result |
|---|---|
| `profile.name` | Rebuilds when the name changes |
| `profile.age` | Rebuilds when the age changes |
| `profile` | Rebuilds for any profile change |
| A newly created list | May rebuild more often than expected |
A common mistake is selecting a new object or collection on every evaluation. Prefer immutable fields, primitive values, or stable value types. Also avoid using select everywhere without measuring the result. It adds comparison logic and can make code harder to read.
Exercise
If a widget displays only profile.name, what should it watch to ignore age-only updates?
Show answer
It should use `ref.watch(profileProvider.select((profile) => profile.name)).