Computed<T> class
A reactive class that computes a value on demand and notifies listeners when the value changes.
This class acts as both a ReactiveNotifier and a Watcher, allowing it to observe dependencies and recompute its value when those dependencies change. The computation is only triggered when value is accessed.
Features:
- Lazy Computation: The value is computed only when accessed.
- Automatic Dependency Tracking: Recomputes when observed values change.
- Efficient Updates: Notifies listeners only if the computed value changes.
Example Usage:
// Create reactive state variables
final count = Ref<int>(0);
final doubleCount = Computed(() => count.value * 2);
// Access computed value (triggers computation)
print(doubleCount.value); // Output: 0
// Update dependency
count.value = 5;
// Computed value updates automatically
print(doubleCount.value); // Output: 10
// Reactivity: Automatically recomputes when `count` changes
watchEffect(() {
print('Double count changed: ${doubleCount.value}');
});
Notes:
- The computation is cached until dependencies change.
- Call
force(value)
to manually override the computed value.
- Inheritance
-
- Object
- ChangeNotifier
- ReactiveNotifier<
T> - Computed
- Mixed-in types
-
- WatcherRaw<
T> - Watcher<
T>
- WatcherRaw<
Constructors
- Computed.new(T _getValue())
-
Creates a new Computed object that computes its value using
getValue
.
Properties
-
computes
↔ Map<
int, Picker> -
A map of computes that are used by this watcher.
getter/setter pairinherited
- currentIndexCompute ↔ int
-
The current index of the compute being used.
getter/setter pairinherited
- dryRun ↔ T Function()
-
A function that is called to track dependencies.
latefinalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- mounted → bool
-
Whether the notifier is mounted.
no setterinherited
- onChange ↔ VoidCallback
-
A callback that is called when the watcher's dependencies change.
latefinalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → T
-
Returns the computed value, computing it if necessary.
no setteroverride
-
watchers
↔ Set<
ReactiveNotifier> -
The set of notifiers that this watcher is listening to.
getter/setter pairinherited
Methods
-
addDepend(
ReactiveNotifier ref) → void -
Adds a dependency to this watcher.
inherited
-
addListener(
VoidCallback listener) → void -
Adds a listener and ensures computation is performed before notifying.
override
-
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
-
dispose2(
) → void -
Disposes of the watcher.
inherited
-
force(
T value) → void - Manually overrides the computed value and forces an update.
-
getCC(
) → Picker? -
Gets the current compute.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notify(
) → void - Notifies listeners of a change.
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
onCleanup(
VoidCallback callback) → void -
Registers a cleanup function to be called when the watcher is disposed.
inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
run(
) → T -
Runs the watcher and returns the result.
inherited
-
select<
U> (U getter(T value)) → Computed< U> -
Creates a Computed that selects a value from the notifier.
inherited
-
setCC(
Picker cc) → Picker -
Sets the current compute.
inherited
-
toString(
) → String -
Returns a string representation of the computed value.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited