Notifier<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 counterPod = notifierPod<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:
Text('count: ${context.watch(counterPod)}')
And finally, we can update the counter by doing:
ElevatedButton(
onTap: () => context.read(counterPod.notifier).increment(),
child: const Text('increment'),
)
The state of Notifier is expected to be initialized synchronously. For asynchronous initializations, see AsyncNotifier.
- Inheritance
-
- Object
- NotifierBase
- Notifier
Constructors
- Notifier()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- ref → Ref
-
The Ref from the pod associated with this notifier.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state ↔ State
-
The current "state" of this Notifier.
getter/setter pair
Methods
-
addListener(
ListenerCallback< State> listener) → RemoveListener - Subscribes to this object.
-
build(
) → State - Initialize a Notifier.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited