protected_notifier_properties
v0.8.0 Warning Riverpod State
Flags access to state, stateOrNull, future or ref on a Notifier from outside the notifier that owns them. These members are part of a notifier’s internal API.
Why use this rule
Section titled “Why use this rule”Reading notifier.state bypasses the provider system: the value is read once, and the reader is never rebuilt when it changes. Writing it from outside moves state transitions out of the notifier, which is where the rest of the codebase expects to find them. Going through the provider gives correct reactivity and keeps mutations in one place.
See also: Riverpod providers
void bad(MyNotifier notifier) { print(notifier.state); // LINT notifier.state = 1; // LINT print(notifier.ref); // LINT}// Read the value through its provider so the widget rebuilds on change.Widget good(WidgetRef ref) { final value = ref.watch(myProvider); return Text('$value');}
// Mutate through a method the notifier exposes.void increment(WidgetRef ref) { ref.read(myProvider.notifier).increment();}
// Inside the notifier itself, the members are free to use.class MyNotifier extends Notifier<int> { @override int build() => 0;
void increment() => state = state + 1;}Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: protected_notifier_properties: trueTo turn it off again:
rules: protected_notifier_properties: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”notifier_build— Classes annotated with @riverpod must define a build method.avoid_public_notifier_properties— Prevent public fields, getters, and setters on Notifier classes.avoid_notifier_constructors— Prevent initialization logic in Notifier constructors.async_value_nullable_pattern— Matching AsyncValue(:final value?) on a nullable value hides a legitimate null result.