value property

T get value

Gets the current computed value.

If read within a reactive context (like an Effect or another Computed), it automatically registers itself as a dependency of that context.

Recalculates the value only if it is marked dirty. If evaluation is already in progress on the call stack, throws a StateError to prevent circular dependencies.

Implementation

T get value {
  // 1. Dependency tracking for any active outer consumer (e.g. Effect or outer Computed)
  final active = activeConsumer;
  if (active != null) {
    addObserver(active);
    if (active is DependencyTracker) {
      active.addDependency(this);
    }
  }

  // 2. Recompute and cache if dirty
  if (_isDirty) {
    if (_isComputing) {
      throw StateError(
          "Circular dependency detected during evaluation of Computed!");
    }
    _isComputing = true;
    pushConsumer(this);
    clearDependencies();
    try {
      _cachedValue = _computeFn();
      _isDirty = false;
    } finally {
      popConsumer();
      _isComputing = false;
    }
  }

  return _cachedValue as T;
}