avoid-calling-notifier-members-inside-build
- Group
- Correctness
- Path
lint/correctness/avoid-calling-notifier-members-inside-build- Default
- On (recommended)
- Origin
- Falcon
- Domain
- riverpod
- Rule
- 27 of 512
Flags calls to Riverpod notifier methods during widget build.
Build must not trigger notifier side effects. Method tear-offs and nested
callbacks are not calls during build; unrelated same-named types and Object
methods (toString and noSuchMethod) are ignored.
Invalid
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart' as hooks;
final countProvider = Provider<int>((ref) => 0);
class Counter extends Notifier<int> { void increment() {} void reset() {} int get value => 0; }
class AsyncCounter extends AsyncNotifier<int> { void refresh() {} }
final counterProvider = Provider<Counter>((ref) => Counter());
final asyncProvider = Provider<AsyncCounter>((ref) => AsyncCounter());
class Screen extends ConsumerWidget {
Object build(Object context, WidgetRef ref) {
ref.read(counterProvider).increment();
final counter = ref.watch(counterProvider);
counter.increment();
counter.reset();
ref.watch(asyncProvider).refresh();
return 0;
}
}
class Arrow extends ConsumerWidget {
Object build(Object context, WidgetRef ref) {
ref.read(counterProvider).increment();
return 0;
}
}
Object consumer() => Consumer(builder: (context, ref, child) {
ref.read(counterProvider).increment();
return 0;
});
class HookScreen extends hooks.HookConsumerWidget {
Object build(Object context, hooks.WidgetRef ref) {
ref.read(counterProvider).increment();
return 0;
}
} Object build(Object context, WidgetRef ref) { ref.read(counterProvider).increment(); final counter = ref.watch(counterProvider); counter.increment(); counter.increment(); counter.reset(); counter.reset(); ref.watch(asyncProvider).refresh(); Object build(Object context, WidgetRef ref) { ref.read(counterProvider).increment();Object consumer() => Consumer(builder: (context, ref, child) { ref.read(counterProvider).increment(); Object build(Object context, hooks.WidgetRef ref) { ref.read(counterProvider).increment();Valid
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart' as hooks;
final countProvider = Provider<int>((ref) => 0);
class Counter extends Notifier<int> { void increment() {} void reset() {} int get value => 0; }
class AsyncCounter extends AsyncNotifier<int> { void refresh() {} }
final counterProvider = Provider<Counter>((ref) => Counter());
final asyncProvider = Provider<AsyncCounter>((ref) => AsyncCounter());
class Service { void increment() {} }
class Screen extends ConsumerWidget {
Object build(Object context, WidgetRef ref) {
final counter = ref.watch(counterProvider);
final callback = () => counter.increment();
void local() { counter.reset(); }
final tearoff = counter.increment;
final service = Service();
service.increment();
counter.toString();
return callback;
}
void action(WidgetRef ref) { ref.read(counterProvider).increment(); }
}
void outside(Counter counter) { counter.increment(); }
class Unrelated {
Object build(Object context, Counter counter) { counter.increment(); return 0; }
}
class DeferredMenu extends ConsumerWidget {
Object build(Object context, WidgetRef ref) =>
PopupMenuButton(itemBuilder: (context) {
ref.read(counterProvider).increment();
return 0;
});
}Configure avoid-calling-notifier-members-inside-build
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-calling-notifier-members-inside-build": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-calling-notifier-members-inside-build: <reason>More correctness rules
- avoid-collection-equality-checksrecommended
Flags equality comparisons between non-constant SDK collections.
- avoid-conditional-hooksrecommendedflutter_hooks
Flags hook calls in conditional branches, short-circuit operands, and loops.
- avoid-constant-assert-conditions
Flags assertions whose Boolean condition is statically constant.
- avoid-constant-conditions
Flags statically constant binary branch, loop, collection, and expression conditions.
- avoid-constant-switches
Flags switch statements and expressions with a statically constant subject.
- avoid-contradictory-expressionsrecommended
Flags pairs of conditions that are always false with
&&or true with||. - avoid-disposing-late-fieldsflutter
Avoid releasing late State fields that may be unset or initialized only by cleanup.
- avoid-duplicate-bloc-event-handlersrecommendedbloc
Flags later registrations of the same event type in a Bloc constructor.