Sub topic 8
Select Only the State a Widget Needs
This chapter assumes basic Dart, StatelessWidget and StatefulWidget, setState, the build method, and the ability to run a Flutter app. It also assumes you know that a state-management provider can notify widgets when its data changes.
A widget should rebuild when the value it displays changes. It does not usually need to rebuild for every change in the object that contains that value. A selector lets the widget subscribe to one smaller part of a larger state object.
Whole-object listening versus selection
Consider a UserModel with several fields:
nameemailavatarUrlisOnline
A widget that displays only the name should not rebuild when isOnline changes. With the provider package, context.watch<UserModel>() listens to the whole model:
final user = context.watch<UserModel>();
return Text(user.name);Any notification from UserModel can rebuild this widget. context.select projects the model into the exact value the widget needs:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class UserName extends StatelessWidget {
const UserName({super.key});
@override
Widget build(BuildContext context) {
final name = context.select<UserModel, String>(
(user) => user.name,
);
return Text(name);
}
}Here, the selector returns a String. The widget rebuilds when the selected name changes. A notification caused only by a changed email or avatar does not rebuild UserName.
The model must notify its listeners when its fields change:
class UserModel extends ChangeNotifier {
String _name = 'Ada';
String _email = '[email protected]';
String get name => _name;
String get email => _email;
void rename(String value) {
if (_name == value) return;
_name = value;
notifyListeners();
}
void changeEmail(String value) {
if (_email == value) return;
_email = value;
notifyListeners();
}
}The provider still sends a notification for either change. The selector then compares the newly selected value with the previous selected value. If the selected value is equal, the dependent widget can remain unchanged.
Choose stable selected values
Select simple, value-like data when possible:
Stringintdoublebool- enums
- immutable objects with correct equality
Avoid creating a new object on every selection unless that object has meaningful equality. For example, selecting a fresh list or map can make the selection appear changed even when its contents are the same. Select one field, or use an immutable value type whose equality compares its fields.
You can select a derived value when that is what the widget needs:
final isSignedIn = context.select<UserModel, bool>(
(user) => user.name.isNotEmpty && user.email.isNotEmpty,
);This widget depends on the resulting Boolean, not on every individual model field. Keep the selector inexpensive. It may run whenever the provider notifies listeners.
How the dependency is recorded
A provider uses Flutter’s inherited-widget mechanism to expose a value below part of the widget tree. When a widget reads that value, Flutter records a dependency between the widget’s element and the provider.
The dependency path for a selector looks like this:
This same idea appears in other state-management libraries. The API names differ, but the design is the same: subscribe to the smallest state slice that determines the widget’s output.
Decide whether selection is worth it
Do not add selectors to every widget automatically. A rebuild is often cheap, especially for a small Text widget. Selection is most useful when:
- The model changes frequently.
- The dependent subtree is large.
- The widget appears many times in a screen.
- Profiling shows that unrelated notifications consume frame time.
- The selected value changes less often than the model as a whole.
First measure rebuilds, then narrow a dependency, and measure again. A selector adds an equality check and some mental overhead. Use it when that cost buys a smaller or less frequent rebuild.
Exercise
A widget displays user.name, but it uses context.watch<UserModel>(). The model frequently changes isOnline. What change should you try first?
Show answer
Replace the whole-model read with context.select<UserModel, String>((user) => user.name), then verify with rebuild logging or DevTools that unrelated changes no longer rebuild the widget.