Search packages, repositories and app categories

Package detailsv2.0.0

simple_observable

Observe value changes using a Future, Stream, and/or a callback.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
199K
Likes
6
Pub points
160

Install package

flutter pub add simple_observable

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

simple_observable is a lightweight Dart package that enables easy observation of value changes through Futures, Streams, or callbacks. It simplifies reactive programming by providing a clean interface to react to data updates without boilerplate code. Ideal for state management in Flutter apps, it supports multiple platforms including Android, iOS, Web, macOS, Windows, and Linux. The package enhances developer productivity by reducing complexity when tracking asynchronous data flows.

Use cases

  • Real-time data updates
  • State management in Flutter apps
  • Event-driven UI updates
  • Handling async data streams
  • Simplifying callback logic

Key features

  • Supports Futures and Streams
  • Callback-based observation
  • Minimal boilerplate
  • Cross-platform compatibility
  • Easy integration with existing code

Suitable for

  • Developers seeking simple state updates
  • Flutter apps with reactive UI needs
  • Projects avoiding heavy state management solutions
  • Teams prioritizing code simplicity
  • Apps requiring lightweight observability

Considerations

  • Not suitable for complex state trees
  • Limited to basic observation patterns
  • No built-in persistence or undo functionality
  • Best for small to medium-scale applications
  • Requires manual handling of subscriptions

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

pub.dev

simple_observable

Observe value changes using a Future, Stream, and/or a callback.

Source

class Observable<T> {
  Observable({T initialValue, this.onChanged, this.checkEquality = true})
      : _value = initialValue;

  /// If true, setting the [value] will only notifiy if the new value is different
  /// than the current value.
  final bool checkEquality;
  final void Function(T value) onChanged;

  var _completer = Completer<T>();

  bool _canceled = false;
  bool get canceled => _canceled;

  T _value;

  /// The current value of this observable.
  T get value => _value;
  set value(T val) {
    if (!canceled && (!checkEquality || _value != val)) {
      _value = val;
      // Delaying notify() allows the Future and Stream to update correctly.
      Future.delayed(Duration(microseconds: 1), () => notify(val));
    }
  }

  /// Alias for [value] setter. Good for passing to a Future or Stream.
  void setValue(T val) => value = val;

  @protected
  @mustCallSuper
  void notify(T val) {
    if (onChanged != null) onChanged(val);
    // Completing with a microtask allows a new completer to be constructed
    // before listeners of [nextValue] are called, allowing them to listen to
    // nextValue again if desired.
    _completer.complete(Future.microtask(() => val));
    _completer = Completer<T>();
  }

  Future<T> get nextValue => _completer.future;
  Stream<T> get values async* {
    while (!canceled) {
      yield await nextValue;
    }
  }

  /// Permanently disables this observable. Further changes to [value] will be
  /// ignored, the outputs [onChanged], [nextValue], and [values] will not be
  /// called again.
  @mustCallSuper
  void cancel() => _canceled = true;
}

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package