Skip to content

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.

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

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
protected_notifier_properties: true

To turn it off again:

many_lints.yaml
rules:
protected_notifier_properties: false

To keep the rule on but skip certain paths, use per-rule exclude.