Skip to main content

avoid-ref-read-inside-build

added in: 1.17.0
🛠
Pro+

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) {
// LINT: Avoid using 'ref.read' inside the 'build' method. Try rewriting the code to use 'ref.watch' instead.
final counter = ref.read(counterProvider);

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');
}
}