WritableComputedImpl<T>.withPrevious constructor
WritableComputedImpl<T>.withPrevious (
- T getter(
- T?
- void setter(
- T
- JoltDebugFn? onDebug,
Creates a writable computed value with a getter that receives the previous value.
Parameters:
getter: Function that computes the value, receiving the previous value (ornullon first computation) as a parametersetter: Function called when the computed value is setonDebug: Optional debug callback for reactive system debugging
Example:
final signal = Signal([5]);
final computed = WritableComputed<int>.withPrevious(
(prev) {
final newValue = signal.value[0] * 2;
if (prev != null && prev == newValue) {
return prev;
}
return newValue;
},
(value) => signal.value = [value ~/ 2],
);
Implementation
factory WritableComputedImpl.withPrevious(
T Function(T?) getter,
void Function(T) setter, {
JoltDebugFn? onDebug,
}) {
late final WritableComputedImpl<T> computed;
T fn() => getter(computed.pendingValue);
computed = WritableComputedImpl(fn, setter, onDebug: onDebug);
return computed;
}