CachedWatcher<T> class
abstract
CachedWatcher
A specialized version of Watcher that adds local caching capabilities. Ideal for use cases where persistent state management is required, such as saving user preferences, caching network responses, or maintaining temporary state across app restarts.
The CachedWatcher extends Watcher and inherits its reactive state management capabilities, while adding the functionality to automatically write to and read from a local cache.
CachedWatcher is designed to be extended for various data types,
with abstract read
and write
methods that need to be implemented in subclasses.
This design allows for flexible and type-specific serialization and deserialization strategies for cached data.
Usage:
To use CachedWatcher, create a subclass for the specific data type you wish to cache.
Implement the read
and write
methods to define how data is serialized and deserialized.
Example Subclass Implementation:
class StringCachedWatcher extends CachedWatcher<String> {
StringCachedWatcher(String initialValue, String key) : super(initialValue, key: key);
@override
String? read(dynamic data) => data as String;
@override
dynamic write(String value) => value;
}
Parameters:
- initialValue: The starting value for the watcher.
- key: A unique string key used for storing and retrieving the cached data.
Abstract Methods:
- read: An abstract method that should be implemented to define how the stored data is read and converted back into the expected type.
- write: An abstract method that should be implemented to define how the state is serialized and stored in the local cache.
Additional Methods:
- startCaching: Enables caching of the watcher's value.
- stopCaching: Disables caching of the watcher's value.
- deleteCache: Clears the cache for this specific CachedWatcher.
- deleteAllCaches: Static method to clear all caches associated with CachedWatcher.
- Inheritance
-
- Object
- ChangeNotifier
- Watcher<
T> - CachedWatcher
- Implementers
- Available Extensions
Constructors
- CachedWatcher(T initialValue, {String? key})
Properties
- cache → Future
-
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- initialValue → T
-
final
- isCaching → bool
-
isCaching
no setter
- 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 setterinherited
- key ↔ String
-
key
latefinal
- 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
finalinherited
-
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 setterinherited - v ↔ T
-
Equivalent to the getter value but in shorter syntax.
getter/setter pairinherited-getteroverride-setter
- value ↔ T
-
The current value stored in this watcher.
getter/setter pairinherited-getteroverride-setter
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
.inherited -
deleteCache(
) → Future< void> - deleteCache
-
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).
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
onChange(
void action(T value)) → VoidCallback -
Registers a callback to be invoked whenever the
ValueNotifier
's value changes.inherited -
read(
dynamic data) → FutureOr< T?> - read
-
refresh(
) → void -
Rebuilds and trigger any listeners of any WatchValue or
watch
attached to that Watcher.inherited -
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 -
inherited
-
startCaching(
) → void - startCaching
-
stopCaching(
) → void - stopCaching
-
toString(
) → String -
A string representation of this object.
inherited
-
updateIf(
bool condition(T), T newValue) → void -
Updates the
ValueNotifier
's value tonewValue
ifcondition
returns true.inherited -
updateOnAction<
R> (R action()) → R -
Will notifyListeners after a specific
action
has been made, and optionally return a resultR
of certain type.inherited -
write(
T value) → FutureOr - write
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
deleteAllCaches(
) → Future< void> - deleteAllCaches