simple_observable
2.0.0, released 6 years ago by defylogic.dev
Observable class to watch value changes via callbacks, Future or Stream.
WASM-readySingle maintainer
What it does
Provides a simple Observable that holds a value and notifies listeners when it changes. It exposes the next change as a Future and a continuous Stream. Supports optional equality checks and cancellation.
AI-written summary of version 2.0.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- Need a tiny, dependency‑free way to react to value changes
- Want to bridge Future/Stream APIs without pulling in a full state‑management library
Skip it when
- Building a large‑scale app that requires scoped providers or advanced state handling
- Need widget‑level integration like Consumer or selectors
Strengths
- Very lightweight – only depends on meta
- Null‑safe and works on all Dart platforms
- Simple API with Future, Stream and callback support
Weaknesses
- No built‑in Flutter widget helpers
- Limited to single value observation, no composition
- No ecosystem features like debugging or devtools
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 1
class Observable<T>{
Observable({T initialValue,this.onChanged,this.checkEquality=true})
: _value=initialValue;
final bool checkEquality;
final void Function(T) onChanged;
var _completer=Completer<T>();
bool _canceled=false;
T _value;
T get value=>_value;
set value(T val){
if(!_canceled && (!checkEquality||_value!=val)){
_value=val;
Future.delayed(Duration(microseconds:1),()=>notify(val));
}
}
void notify(T val){
onChanged?.call(val);
_completer.complete(Future.microtask(()=>val));
_completer=Completer<T>();
}
Future<T> get nextValue=>_completer.future;
Stream<T> get values async*{while(!_canceled) yield await nextValue;}
void cancel()=>_canceled=true;
}Alternatives
Packages that do the same job and could replace simple_observable.
Works with
Packages commonly used together with simple_observable: ones it builds on, ones built on it, and its own family.