AutoDisposeNotifier<State> class
abstract
A class which exposes a state that can change over time.
For example, Notifier can be used to implement a counter by doing:
final counterProvider = NotifierProvider<Counter, int>(Counter.new);
class Counter extends Notifier<int> {
@override
int build() {
// Inside "build", we return the initial state of the counter.
return 0;
}
void increment() {
state++;
}
}
We can then listen to the counter inside widgets by doing:
Consumer(
builder: (context, ref) {
return Text('count: ${ref.watch(counterProvider)}');
},
)
And finally, we can update the counter by doing:
Consumer(
builder: (context, ref) {
return ElevatedButton(
onTap: () => ref.read(counterProvider.notifier).increment(),
child: const Text('increment'),
);
},
)
The state of Notifier is expected to be initialized synchronously. For asynchronous initializations, see AsyncNotifier.
When using autoDispose or family, your notifier type changes.
Instead of extending Notifier, you should extend either:
- AutoDisposeNotifier for
autoDispose - FamilyNotifier for
family - AutoDisposeFamilyNotifier for
autoDispose.family
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
ref
→ AutoDisposeNotifierProviderRef<
State> -
The Ref from the provider associated with this Notifier.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state ↔ State
-
The value currently exposed by this Notifier.
getter/setter pairinherited
- stateOrNull → State?
-
The value currently exposed by this Notifier.
no setterinherited
Methods
-
build(
) → State - Initialize an AsyncNotifier.
-
listenSelf(
void listener(State? previous, State next), {void onError(Object error, StackTrace stackTrace)?}) → void -
Listens to changes on the value exposed by this provider.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
updateShouldNotify(
State previous, State next) → bool -
A method invoked when the state exposed by this Notifier changes.
It compares the previous and new value, and return whether listeners
should be notified.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited