Computed<T> class
A derived signal that automatically recalculates when dependencies change.
Computed creates a read-only signal whose value is computed from other signals. It automatically detects dependencies and recalculates whenever any dependency changes.
Key Features
- Automatic dependency tracking: No need to manually list dependencies
- Lazy evaluation: Only computes when accessed and listeners exist
- Error handling: Captures computation errors gracefully
- Circular dependency detection: Throws clear error on cycles
Basic Usage
class CalculatorController extends NeuronController {
late final width = Signal<double>(10).bind(this);
late final height = Signal<double>(20).bind(this);
// Dependencies detected automatically!
late final area = Computed<double>(
() => width.val * height.val,
).bind(this);
}
Error Handling
If the computation throws, the error is captured and can be checked:
late final ratio = Computed<double>(() {
if (height.val == 0) throw ArgumentError('Cannot divide by zero');
return width.val / height.val;
}).bind(this);
// Later:
if (ratio.hasError) {
print('Error: ${ratio.error}');
} else {
print('Ratio: ${ratio.val}');
}
Chaining
Computed signals can depend on other computed signals:
late final doubled = Computed<int>(() => count.val * 2).bind(this);
late final quadrupled = Computed<int>(() => doubled.val * 2).bind(this);
Initial Value
Provide an initial value to defer computation:
late final expensive = Computed<Data>(
() => computeExpensiveValue(),
initialValue: Data.empty(),
).bind(this);
Note: Computed signals are read-only. You cannot set their value. To change them, modify their dependencies.
See also:
- Inheritance
-
- Object
- NeuronAtom<
T> - Computed
- Available extensions
Constructors
- Computed(T _compute(), {T? initialValue, String? debugLabel, bool equals(T a, T b)?, T guard(T current, T next)?, VoidCallback? onListen, VoidCallback? onCancel})
- Creates a computed signal with automatic dependency tracking.
-
Computed.withDependencies(T compute(), List<
NeuronAtom> dependencies, {String? debugLabel, bool equals(T a, T b)?, T guard(T current, T next)?, VoidCallback? onListen, VoidCallback? onCancel}) -
Legacy constructor for backward compatibility.
factory
Properties
- debugLabel → String?
-
Debug label for identification in DevTools.
final
- equals → bool Function(T a, T b)?
-
Custom equality function to determine if value has changed.
finalinherited
- error → Object?
-
The error from the last computation, if any.
no setter
- guard → T Function(T current, T next)?
-
Value guard/transformer called before setting a new value.
finalinherited
- hasError → bool
-
Whether this computed has an error from the last computation.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether this atom has any listeners.
no setterinherited
- initialValue → T
-
The initial value of the atom.
no setterinherited
- isDisposed → bool
-
Whether this atom has been disposed.
no setterinherited
- onCancel → VoidCallback?
-
Callback invoked when the last listener unsubscribes.
finalinherited
- onListen → VoidCallback?
-
Callback invoked when the first listener subscribes.
finalinherited
- previousValue → T?
-
The previous value of the atom (before the last change).
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stackTrace → StackTrace?
-
The stack trace from the last computation error, if any.
no setter
- val → T
-
Short alias for the current value.
no setter
- value ↔ T
-
The current value of the atom.
getter/setter pairoverride
Methods
-
addListener(
VoidCallback listener) → void -
Adds a listener to be called when the value changes.
inherited
-
bind(
NeuronController parent) → T -
Available on T, provided by the SignalBinding extension
Registers this notifier to be disposed withparent. -
dispose(
) → void -
Disposes the atom, removing all listeners.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Manually notifies all listeners.
inherited
-
onActive(
) → void -
Called when the first listener is added - sets up dependency subscriptions.
override
-
onInactive(
) → void -
Called when the last listener is removed - tears down subscriptions.
override
-
removeListener(
VoidCallback listener) → void -
Removes a previously added listener.
inherited
-
reset(
) → void -
Resets the atom to its initial value.
inherited
-
select<
R> (R selector(T value)) → NeuronAtom< R> -
Creates a new atom that selects a part of this atom's value.
inherited
-
subscribe(
VoidCallback listener) → VoidCallback -
Adds a listener and returns a callback that cancels the subscription when called.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited