SwitchIfEmptyStreamTransformer<S> class

When the original stream emits no items, this operator subscribes to the given fallback stream and emits items from that stream instead.

This can be particularly useful when consuming data from multiple sources. For example, when using the Repository Pattern. Assuming you have some data you need to load, you might want to start with the fastest access point and keep falling back to the slowest point. For example, first query an in-memory database, then a database on the file system, then a network call if the data isn't on the local machine.

This can be achieved quite simply with switchIfEmpty!

Example

// Let's pretend we have some Data sources that complete without emitting
// any items if they don't contain the data we're looking for
Stream<Data> memory;
Stream<Data> disk;
Stream<Data> network;

// Start with memory, fallback to disk, then fallback to network.
// Simple as that!
Stream<Data> getThatData =
    memory.switchIfEmpty(disk).switchIfEmpty(network);
Inheritance

Constructors

SwitchIfEmptyStreamTransformer(Stream<S> fallbackStream)
Constructs a StreamTransformer which, when the source Stream emits no events, switches over to fallbackStream.

Properties

fallbackStream Stream<S>
The Stream which will be used as fallback, if the source Stream is empty.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

bind(Stream<S> stream) Stream<S>
Transforms the provided stream.
override
cast<RS, RT>() StreamTransformer<RS, RT>
Provides a StreamTransformer<RS, RT> view of this stream transformer.
inherited
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