Rx class abstract
A utility class that provides static methods to create the various Streams provided by RxDart.
Example
Rx.combineLatest([
Stream.value('a'),
Stream.fromIterable(['b', 'c', 'd'])
], (list) => list.join())
.listen(print); // prints 'ab', 'ac', 'ad'
Learning RxDart
This library contains documentation and examples for each method. In addition, more complex examples can be found in the RxDart github repo demonstrating how to use RxDart with web, command line, and Flutter applications.
Additional Resources
In addition to the RxDart documentation and examples, you can find many more articles on Dart Streams that teach the fundamentals upon which RxDart is built.
- Asynchronous Programming: Streams
- Single-Subscription vs. Broadcast Streams
- Creating Streams in Dart
- Testing Streams: Stream Matchers
Dart Streams vs Traditional Rx Observables
In ReactiveX, the Observable class is the heart of the ecosystem. Observables represent data sources that emit 'items' or 'events' over time. Dart already includes such a data source: Streams.
In order to integrate fluently with the Dart ecosystem, Rx Dart does not provide a Stream class, but rather adds functionality to Dart Streams. This provides several advantages:
- RxDart works with any API that expects a Dart Stream as an input.
- No need to implement or replace the many methods and properties from the core Stream API.
- Ability to create Streams with language-level syntax.
Overall, we attempt to follow the ReactiveX spec as closely as we can, but
prioritize fitting in with the Dart ecosystem when a trade-off must be made.
Therefore, there are some important differences to note between Dart's
Stream class and standard Rx Observable
.
First, Cold Observables exist in Dart as normal Streams, but they are single-subscription only. In other words, you can only listen a Stream once, unless it is a hot (aka broadcast) Stream. If you attempt to listen to a cold Stream twice, a StateError will be thrown. If you need to listen to a stream multiple times, you can simply create a factory function that returns a new instance of the stream.
Second, many methods contained within, such as first
and last
do not
return a Single
nor an Observable
, but rather must return a Dart Future.
Luckily, Dart's Future
class is conceptually similar to Single
, and can
be easily converted back to a Stream using the myFuture.asStream()
method
if needed.
Third, Streams in Dart do not close by default when an error occurs. In Rx, an Error causes the Observable to terminate unless it is intercepted by an operator. Dart has mechanisms for creating streams that close when an error occurs, but the majority of Streams do not exhibit this behavior.
Fourth, Dart streams are asynchronous by default, whereas Observables are synchronous by default, unless you schedule work on a different Scheduler. You can create synchronous Streams with Dart, but please be aware the the default is simply different.
Finally, when using Dart Broadcast Streams (similar to Hot Observables),
please know that onListen
will only be called the first time the
broadcast stream is listened to.
Constructors
- Rx()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
combineLatest<
T, R> (Iterable< Stream< streams, R combiner(List<T> >T> values)) → Stream<R> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. This is helpful when you need to combine a dynamic number of Streams. -
combineLatest2<
A, B, T> (Stream< A> streamA, Stream<B> streamB, T combiner(A a, B b)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest3<
A, B, C, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, T combiner(A a, B b, C c)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest4<
A, B, C, D, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, T combiner(A a, B b, C c, D d)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest5<
A, B, C, D, E, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, T combiner(A a, B b, C c, D d, E e)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest6<
A, B, C, D, E, F, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, T combiner(A a, B b, C c, D d, E e, F f)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest7<
A, B, C, D, E, F, G, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, T combiner(A a, B b, C c, D d, E e, F f, G g)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest8<
A, B, C, D, E, F, G, H, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, T combiner(A a, B b, C c, D d, E e, F f, G g, H h)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatest9<
A, B, C, D, E, F, G, H, I, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, Stream<I> streamI, T combiner(A a, B b, C c, D d, E e, F f, G g, H h, I i)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function whenever any of the stream sequences emits an item. -
combineLatestList<
T> (Iterable< Stream< streams) → Stream<T> >List< T> > - Merges the given Streams into a single Stream that emits a List of the values emitted by the source Stream. This is helpful when you need to combine a dynamic number of Streams.
-
concat<
T> (Iterable< Stream< streams) → Stream<T> >T> - Concatenates all of the specified stream sequences, as long as the previous stream sequence terminated successfully.
-
concatEager<
T> (Iterable< Stream< streams) → Stream<T> >T> - Concatenates all of the specified stream sequences, as long as the previous stream sequence terminated successfully.
-
defer<
T> (Stream< T> streamFactory(), {bool reusable = false}) → Stream<T> - The defer factory waits until an observer subscribes to it, and then it creates a Stream with the given factory function.
-
forkJoin<
T, R> (Iterable< Stream< streams, R combiner(List<T> >T> values)) → Stream<R> - Creates a Stream where all last events of existing stream(s) are piped through a sink-transformation.
-
forkJoin2<
A, B, T> (Stream< A> streamA, Stream<B> streamB, T combiner(A a, B b)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin3<
A, B, C, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, T combiner(A a, B b, C c)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin4<
A, B, C, D, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, T combiner(A a, B b, C c, D d)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin5<
A, B, C, D, E, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, T combiner(A a, B b, C c, D d, E e)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin6<
A, B, C, D, E, F, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, T combiner(A a, B b, C c, D d, E e, F f)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin7<
A, B, C, D, E, F, G, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, T combiner(A a, B b, C c, D d, E e, F f, G g)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin8<
A, B, C, D, E, F, G, H, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, T combiner(A a, B b, C c, D d, E e, F f, G g, H h)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoin9<
A, B, C, D, E, F, G, H, I, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, Stream<I> streamI, T combiner(A a, B b, C c, D d, E e, F f, G g, H h, I i)) → Stream<T> -
Merges the given Streams into a single Stream sequence by using the
combiner
function when all of the stream sequences emits their last item. -
forkJoinList<
T> (Iterable< Stream< streams) → Stream<T> >List< T> > - Merges the given Streams into a single Stream that emits a List of the last values emitted by the source stream(s). This is helpful when you need to forkJoin a dynamic number of Streams.
-
fromCallable<
T> (FutureOr< T> callable(), {bool reusable = false}) → Stream<T> - Returns a Stream that, when listening to it, calls a function you specify and then emits the value returned from that function.
-
merge<
T> (Iterable< Stream< streams) → Stream<T> >T> -
Flattens the items emitted by the given
streams
into a single Stream sequence. -
never<
T> () → Stream< T> - Returns a non-terminating stream sequence, which can be used to denote an infinite duration.
-
race<
T> (Iterable< Stream< streams) → Stream<T> >T> -
Given two or more source
streams
, emit all of the items from only the first of thesestreams
to emit an item or notification. -
range(
int startInclusive, int endInclusive) → Stream< int> - Returns a Stream that emits a sequence of Integers within a specified range.
-
repeat<
T> (Stream< T> streamFactory(int repeatIndex), [int? count]) → Stream<T> - Creates a Stream that will recreate and re-listen to the source Stream the specified number of times until the Stream terminates successfully.
-
retry<
T> (Stream< T> streamFactory(), [int? count]) → Stream<T> - Creates a Stream that will recreate and re-listen to the source Stream the specified number of times until the Stream terminates successfully.
-
retryWhen<
T> (Stream< T> streamFactory(), Stream<void> retryWhenFactory(Object error, StackTrace stackTrace)) → Stream<T> - Creates a Stream that will recreate and re-listen to the source Stream when the notifier emits a new value. If the source Stream emits an error or it completes, the Stream terminates.
-
sequenceEqual<
A, B> (Stream< A> stream, Stream<B> other, {bool equals(A a, B b)?, bool errorEquals(ErrorAndStackTrace, ErrorAndStackTrace)?}) → Stream<bool> -
Determine whether two Streams emit the same sequence of items.
You can provide an optional
equals
handler to determine equality. -
switchLatest<
T> (Stream< Stream< streams) → Stream<T> >T> - Convert a Stream that emits Streams (aka a 'Higher Order Stream') into a single Stream that emits the items emitted by the most-recently-emitted of those Streams.
-
timer<
T> (T value, Duration duration) → Stream< T> - Emits the given value after a specified amount of time.
-
using<
T, R> ({required FutureOr< R> resourceFactory(), required Stream<T> streamFactory(R), required FutureOr<void> disposer(R)}) → Stream<T> -
When listener listens to it, creates a resource object from resource factory function,
and creates a Stream from the given factory function and resource as argument.
Finally when the stream finishes emitting items or stream subscription
is cancelled (call StreamSubscription.cancel or
Stream.listen(cancelOnError: true)
), call the disposer function on resource object. -
zip<
T, R> (Iterable< Stream< streams, R zipper(List<T> >T> values)) → Stream<R> - Merges the iterable streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip2<
A, B, T> (Stream< A> streamA, Stream<B> streamB, T zipper(A a, B b)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip3<
A, B, C, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, T zipper(A a, B b, C c)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip4<
A, B, C, D, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, T zipper(A a, B b, C c, D d)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip5<
A, B, C, D, E, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, T zipper(A a, B b, C c, D d, E e)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip6<
A, B, C, D, E, F, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, T zipper(A a, B b, C c, D d, E e, F f)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip7<
A, B, C, D, E, F, G, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, T zipper(A a, B b, C c, D d, E e, F f, G g)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip8<
A, B, C, D, E, F, G, H, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, T zipper(A a, B b, C c, D d, E e, F f, G g, H h)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zip9<
A, B, C, D, E, F, G, H, I, T> (Stream< A> streamA, Stream<B> streamB, Stream<C> streamC, Stream<D> streamD, Stream<E> streamE, Stream<F> streamF, Stream<G> streamG, Stream<H> streamH, Stream<I> streamI, T zipper(A a, B b, C c, D d, E e, F f, G g, H h, I i)) → Stream<T> - Merges the specified streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.
-
zipList<
T> (Iterable< Stream< streams) → Stream<T> >List< T> > - Merges the iterable streams into one stream sequence using the given zipper function whenever all of the stream sequences have produced an element at a corresponding index.