avoid-assigning-notifiers
Warns when a notifier instance obtained from ref.read is assigned to a variable.
Assigning notifier instances to variables is generally not recommended, as it can lead to stale references to already unmounted notifiers (especially, when it comes to async gaps). Accessing an unmounted notifier can lead to unexpected behavior and exceptions.
Instead, consider always using ref.read to get the latest reference.
Example
❌ Bad:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(context) {
return FooWidget(
onChange: (value) async {
// LINT: Assigning notifiers to a variable can lead to accessing unmounted notifiers.
// Try using 'ref.read' in all places.
final value = ref.read<MyNotifier>(counterProvider.notifier);
await ...
value.fn(); // can be unmounted
},
);
}
}
✅ Good:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(context) {
return FooWidget(
onChange: (value) async {
ref.read<MyNotifier>(counterProvider.notifier).someFn();
await ...
ref.read<MyNotifier>(counterProvider.notifier).fn(); // always the latest state
},
);
}
}