avoid-ref-read-inside-build
Warns when ref.read
is used inside the build
method.
ref.read
is designed to read the value one time which is, if done inside the build
method, can lead to missing the event when the provided value changes.
Example
❌ Bad:
class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget({super.key});
Widget build(BuildContext context, WidgetRef ref) {
final counter = ref.read(counterProvider); // LINT
return Text('$counter');
}
}
✅ Good:
class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget({super.key});
Widget build(BuildContext context, WidgetRef ref) {
final counter = ref.watch(counterProvider);
return Text('$counter');
}
}