RxDart is a reactive functional programming library for Google Dart, based on ReactiveX.
Google Dart comes with a very decent Streams API out-of-the-box; rather than attempting to provide an alternative to this API, RxDart adds functionality on top of it.
Dart 1.0 is supported until release 0.15.x, version 0.16.x is no longer backwards compatible and requires the Dart SDK 2.0
void main() { const konamiKeyCodes = const <int>[ KeyCode.UP, KeyCode.UP, KeyCode.DOWN, KeyCode.DOWN, KeyCode.LEFT, KeyCode.RIGHT, KeyCode.LEFT, KeyCode.RIGHT, KeyCode.B, KeyCode.A]; final result = querySelector('#result'); final keyUp = new Observable<KeyboardEvent>(document.onKeyUp); keyUp .map((event) => event.keyCode) .bufferCount(10, 1) .where((lastTenKeyCodes) => const IterableEquality<int>().equals(lastTenKeyCodes, konamiKeyCodes)) .listen((_) => result.innerHtml = 'KONAMI!'); }
RxDart's Observables extends the Stream class. This has two major implications:
Finally, the Observable class & operators are simple wrappers around Stream
and StreamTransformer
classes. All underlying implementations can be used free of the Observable class, and are exposed in their own libraries. They are linked to below.
Generally speaking, creating a new Observable is either done by wrapping a Dart Stream using the top-level constructor new Observable()
, or by calling a factory method on the Observable class. But to better support Dart‘s strong mode, combineLatest
and zip
have been pulled apart into fixed-length constructors. These methods are supplied as static methods, since Dart’s factory methods don't support generic types.
var myObservable = new Observable(myStream);
var myObservable = new Observable.merge([myFirstStream, mySecondStream]);
var myObservable = Observable.combineLatest3( myFirstStream, mySecondStream, myThirdStream, (firstData, secondData, thirdData) => print("$firstData $secondData $thirdData"));
asyncExpand
)A full list of all methods and properties including those provided by the Dart Stream API (such as first
, asyncMap
, etc), can be seen by examining the DartDocs
var myObservable = new Observable(myStream) .bufferCount(5) .distinct();
Web and command-line examples can be found in the example
folder.
In order to run the web examples, please follow these steps:
pub get
pub run build_runner serve example
In order to run the command line example, please follow these steps:
pub get
dart example/example.dart 10
In order to run the flutter example, you must have Flutter installed. For installation instructions, view the online documentation.
cd
into the example/flutter/github_search
directoryflutter doctor
to ensure you have all Flutter dependencies working.flutter packages get
flutter run
Refer to the Changelog to get all release notes.