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 Notifier documentation
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”To disable this rule:
plugins: many_lints: diagnostics: avoid_public_notifier_properties: false