Watcher<T> class
A ChangeNotifier that holds a single value.
When value is replaced with something that is not equal to the old value as evaluated by the equality operator ==, this class notifies its listeners.
When safeMode is enabled, and this isDisposed, any notifyListeners, or setting new value will be ignored.
Limitations
Because this class only notifies listeners when the value's identity changes, listeners will not be notified when mutable state within the value itself changes.
While there is custom watcher types that helps reusing this limitation like ListWatcher, MapWatcher, SetWatcher, etc, When working with custom objects/types like changing variable inside your instance, you need to manually using refresh, or notifyListeners.
As a result, this class is best used with only immutable data types.
For mutable data types, consider using the watcher_classes
or extending ChangeNotifier directly.
- Inheritance
-
- Object
- ChangeNotifier
- Watcher
- Implemented types
- Implementers
- Available extensions
Constructors
- Watcher(T _value, {bool safeMode = true})
- Creates a ChangeNotifier that wraps this value.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- isDisposed → bool
-
Flag which returns true if you called the dispose method.
It is useful when you want to safely do some operations on the Watcher without getting an exception if it gets disposed.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- safeMode → bool
-
safeMode if enabled will not allow any setting of value or notifying any listeners if the Watcher isDisposed
final
-
stream
→ Stream<
T> -
Converts the Watcher into a Stream. This stream emits values whenever the
value changes. The use of
distinct
ensures that consecutive duplicate values are filtered out, thus the stream only emits when the value actually changes.no setter - v ↔ T
-
Equivalent to the getter value but in shorter syntax.
getter/setter pair
- value ↔ T
-
The current value stored in this watcher.
getter/setter pairoverride-getter
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
debounce(
Duration duration, void action(T value)) → VoidCallback -
Registers a debounced callback which is invoked only after the notifier's value
is stable for the specified
duration
. -
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener will throw after the object is disposed).
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
override
-
onChange(
void action(T value)) → VoidCallback -
Registers a callback to be invoked whenever the
ValueNotifier
's value changes. -
refresh(
) → void - Rebuilds and trigger any listeners of any WatchValue or watch attached to that Watcher.
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
setWithoutNotify(
T newValue) → void -
toString(
) → String -
A string representation of this object.
override
-
updateIf(
bool condition(T), T newValue) → void -
Updates the
ValueNotifier
's value tonewValue
ifcondition
returns true. -
updateOnAction<
R> (R action()) → R -
Will notifyListeners after a specific
action
has been made, and optionally return a resultR
of certain type.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited