avoid-calling-notifier-members-inside-build
Warns when a Notifier
(or AsyncNotifier
) member is called inside the build
method.
Calling notifier methods in build
can lead to unwanted side effects and inefficient code, as the build
method can be invoked frequently.
Example
❌ Bad:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(BuildContext context) {
final counter = ref.watch<Counter>(provider.notifier);
counter.increment(); // LINT: Avoid using notifier members inside the 'build' method.
}
}
✅ Good:
class HomeConsumerState extends ConsumerState<HomeConsumerStatefulWidget> {
Widget build(BuildContext context) {
final counter = ref.watch<Counter>(provider.notifier);
return Button(onTap: counter.increment);
}
}