value property

T get value

Gets the current value and tracks the observer if present.

When accessed within a reactive context (e.g., inside an Observer widget), this method automatically registers the dependency.

Implementation

T get value {
  final currentObserver = ObservableTracker.instance.currentObserver;
  if (currentObserver != null) {
    if (!_listeners.contains(currentObserver)) {
      _listeners.add(currentObserver);
    }
    ObservableTracker.instance._registerDependency(currentObserver, this);
  }
  return _value;
}
set value (T newValue)

Sets a new value and notifies all registered listeners if the value has changed.

Implementation

set value(T newValue) {
  if (_value != newValue) {
    _value = newValue;
    for (final listener in _listeners) {
      try {
        listener();
      } catch (e, stack) {
        // Optional: log the error
        print('Observable listener error: $e\n$stack');
      }
    }
  }
}