Read store state in widgets
Reuse the app's typed BuildContext extension. If it is missing, define
select<R>(R Function(AppState) selector) using getSelect<AppState, R>(selector)
and AppState read() using getRead<AppState>().
- Use
context.selectinbuildordidChangeDependenciesto subscribe to the
values that drive the UI. Select the smallest useful value. For several values,
use separate selections or a Dart record.
- Use
context.read()in callbacks andinitStatefor a current snapshot without a
subscription. When a callback needs fresh state, read it inside the callback.
- Keep selectors synchronous and pure. Use the supplied state and keep each
context.select outside other selectors. Select list data in the widget's build
and pass it to item builders.
This widget uses AppState and the extension from the setup skill.
class CounterSummary extends StatelessWidget {
const CounterSummary({required this.onInspect, super.key});
final ValueChanged<int> onInspect;
@override
Widget build(BuildContext context) {
final (:counter, :caption) = context.select(
(st) => (counter: st.counter, caption: st.caption),
);
return TextButton(
onPressed: () => onInspect(context.read().counter),
child: Text('$caption: $counter'),
);
}
}