avoid_public_notifier_properties
v0.4.0 Warning Bloc / Riverpod
This rule flags public properties (fields, getters, and setters) on Notifier and AsyncNotifier subclasses, except for the built-in state property. Public methods, private properties, static properties, and overrides are all allowed.
Why use this rule
Section titled “Why use this rule”Riverpod Notifiers are designed to expose a single reactive state property. When you add extra public getters or fields, consumers can read stale values that don’t trigger rebuilds, leading to UI inconsistencies. Instead, consolidate all data into a model class used as the state type. This keeps the reactive contract intact and makes state changes predictable.
See also: Riverpod providers
import 'package:riverpod/riverpod.dart';
// Public getter exposes state outside the reactive `state` propertyclass BadNotifier extends Notifier<int> { int get publicGetter => 0;
@override int build() => 0;}
// Public field on a Notifierclass BadNotifier2 extends Notifier<int> { int publicField = 0;
@override int build() => 0;}
// Public setter on a Notifierclass BadNotifier3 extends Notifier<int> { int _value = 0;
set publicSetter(int value) => _value = value;
@override int build() => _value;}import 'package:riverpod/riverpod.dart';
// Consolidate state into a model classclass MyState { final int left; final int right; MyState(this.left, this.right);}
class GoodNotifier extends Notifier<MyState> { @override MyState build() => MyState(0, 1);}
// Private properties are fineclass GoodNotifier2 extends Notifier<int> { int _privateField = 0; int get _privateGetter => _privateField;
@override int build() => _privateGetter;}
// Public methods are allowed (only properties are flagged)class GoodNotifier3 extends Notifier<int> { void increment() => state++;
@override int build() => 0;}Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_public_notifier_properties: trueTo turn it off again:
rules: avoid_public_notifier_properties: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_notifier_constructors— Prevent initialization logic in Notifier constructors.protected_notifier_properties— A Notifier’s state, ref and future should not be used from outside the notifier.notifier_build— Classes annotated with @riverpod must define a build method.avoid_bloc_public_methods— Prevent public methods, getters, and setters in Bloc classes.