peekCached property

  1. @override
T get peekCached
override

Returns the cached computed value without establishing a reactive dependency.

This method returns the cached value directly if available, without recomputing. Only computes the value if no cache exists (initial state).

Difference from peek:

  • peek: Always recomputes the value if needed, ensuring you get the latest result (though still without establishing dependencies)
  • peekCached: Returns the cached value immediately if available, only computing when no cache exists. This is more efficient when you just need to check the last computed value, but the value may be stale if dependencies have changed since the last computation.

Use this when you need a quick, efficient access to the last computed value and don't care if it might be slightly out of date.

Example:

final computed = Computed(() => expensiveCalculation());
print(computed.peekCached); // Returns cached value immediately if available

Implementation

@override
T get peekCached {
  assert(!isDisposed, "Computed is disposed");

  if (flags == ReactiveFlags.none) {
    return untracked(() => getComputed(this));
  }

  return pendingValue as T;
}