use-ref-read-synchronously
Warns when ref.read
is called past an await point (also known as asynchronous gap).
Calling ref.read
after an await
can lead to issues such as reading a stale or unintended state, especially if the widget is no longer mounted.
Example
❌ Bad:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(context) {
return FooWidget(
onChange: (value) async {
ref.read<WithAsync>();
await fetch();
// LINT: Avoid calling 'ref.read' past an await point without checking if the widget is mounted.
ref.read<WithAsync>();
},
);
}
}
✅ Good:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(context) {
return FooWidget(
onChange: (value) async {
ref.read<WithAsync>();
await fetch();
if (mounted) {
// Correct, ensures the widget is mounted first
ref.read<WithAsync>();
}
},
);
}
}