ComputedImpl<T>.withPrevious constructor
ComputedImpl<T>.withPrevious (
- T getter(
- T?
- EqualFn? equals,
- JoltDebugOption? debug,
Creates a 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 parameterdebug: Optional debug options
Example:
final signal = Signal<List<int>>([1, 2, 3]);
final computed = Computed<List<int>>.withPrevious((prev) {
final newList = List<int>.from(signal.value);
if (prev != null &&
prev.length == newList.length &&
prev.every((item) => newList.contains(item))) {
return prev; // Return previous to maintain stability
}
return newList;
});
Implementation
factory ComputedImpl.withPrevious(
T Function(T?) getter, {
EqualFn? equals,
JoltDebugOption? debug,
}) {
late final ComputedImpl<T> computed;
T fn() => getter(computed.pendingValue);
computed = ComputedImpl(fn, debug: debug);
return computed;
}