Notifier<ValueT> class abstract Notifiers

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.

Inheritance
Available extensions

Constructors

Notifier.new()

Properties

hashCode int
The hash code for this object.
no setterinherited
ref Ref
The Ref associated with this notifier.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state ↔ ValueT
The value currently exposed by this notifier.
getter/setter pairinherited
stateOrNull → ValueT?
The value currently exposed by this Notifier.
no setterinherited

Methods

build() → ValueT
Initialize a Notifier.
listenSelf(void listener(ValueT? previous, ValueT next), {void onError(Object error, StackTrace stackTrace)?}) → RemoveListener
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
persist<KeyT, EncodedT>(FutureOr<Storage<KeyT, EncodedT>> storage, {required KeyT key, required EncodedT encode(ValueT state), required ValueT decode(EncodedT encoded), StorageOptions options = const StorageOptions()}) PersistResult

Available on AnyNotifier<StateT, ValueT>, provided by the NotifierPersistX extension

Persist the state of a provider to a database.
runBuild() → void
Executes Notifier.build.
override
toString() String
A string representation of this object.
inherited
updateShouldNotify(ValueT previous, ValueT 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