A source of asynchronous data events.
A Stream provides a way to receive a sequence of events. Each event is either a data event or an error event, representing the result of a single computation. When the events provided by a Stream have all been sent, a single "done" event will mark the end.
You can listen on a stream to make it start generating events, and to set up listeners that receive the events. When you listen, you receive a StreamSubscription object which is the active object providing the events, and which can be used to stop listening again, or to temporarily pause events from the subscription.
There are two kinds of streams: "Single-subscription" streams and "broadcast" streams.
A single-subscription stream allows only a single listener during the whole lifetime of the stream. It doesn't start generating events until it has a listener, and it stops sending events when the listener is unsubscribed, even if the source of events could still provide more.
Listening twice on a single-subscription stream is not allowed, even after the first subscription has been canceled.
Single-subscription streams are generally used for streaming chunks of larger contiguous data like file I/O.
A broadcast stream allows any number of listeners, and it fires its events when they are ready, whether there are listeners or not.
Broadcast streams are used for independent events/observers.
If several listeners want to listen to a single subscription stream, use asBroadcastStream to create a broadcast stream on top of the non-broadcast stream.
On either kind of stream, stream transformations, such as where and skip, return the same type of stream as the one the method was called on, unless otherwise noted.
When an event is fired, the listener(s) at that time will receive the event. If a listener is added to a broadcast stream while an event is being fired, that listener will not receive the event currently being fired. If a listener is canceled, it immediately stops receiving events.
When the "done" event is fired, subscribers are unsubscribed before receiving the event. After the event has been sent, the stream has no subscribers. Adding new subscribers to a broadcast stream after this point is allowed, but they will just receive a new "done" event as soon as possible.
Stream subscriptions always respect "pause" requests. If necessary they need to buffer their input, but often, and preferably, they can simply request their input to pause too.
The default implementation of isBroadcast returns false.
A broadcast stream inheriting from Stream must override isBroadcast
to return true
.
Constructors
- Stream()
- Stream.empty()
-
Creates an empty broadcast stream.
constfactory - Stream.eventTransformed(Stream source, EventSink mapSink(EventSink<T> sink))
-
Creates a stream where all events of an existing stream are piped through a sink-transformation.
factory - Stream.fromFuture(Future<T> future)
-
Creates a new single-subscription stream from the future.
factory - Stream.fromFutures(Iterable<Future<T>> futures)
-
Create a stream from a group of futures.
factory - Stream.fromIterable(Iterable<T> data)
-
Creates a single-subscription stream that gets its data from
data
.factory - Stream.periodic(Duration period, [ T computation(int computationCount) ])
-
Creates a stream that repeatedly emits events at
period
intervals.factory
Properties
- first → Future<T>
-
Returns the first element of the stream.
read-only - isBroadcast → bool
-
Whether this stream is a broadcast stream.
read-only - isEmpty → Future<bool>
-
Reports whether this stream contains any elements.
read-only - last → Future<T>
-
Returns the last element of the stream.
read-only - length → Future<int>
-
Counts the elements in the stream.
read-only - single → Future<T>
-
Returns the single element.
read-only - hashCode → int
-
The hash code for this object.
read-only, inherited - runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited
Methods
-
any(
bool test(T element)) → Future<bool> -
Checks whether
test
accepts any element provided by this stream. -
asBroadcastStream(
{void onListen(StreamSubscription<T> subscription), void onCancel(StreamSubscription<T> subscription) }) → Stream<T> -
Returns a multi-subscription stream that produces the same events as this.
-
asyncExpand<E>(
Stream<E> convert(T event)) → Stream<E> -
Creates a new stream with the events of a stream per original event.
-
asyncMap<E>(
dynamic convert(T event)) → Stream<E> -
Creates a new stream with each data event of this stream asynchronously mapped to a new event.
-
contains(
Object needle) → Future<bool> -
Checks whether
needle
occurs in the elements provided by this stream. -
distinct(
[bool equals(T previous, T next) ]) → Stream<T> -
Skips data events if they are equal to the previous data event.
-
drain<E>(
[E futureValue ]) → Future<E> -
Discards all data on the stream, but signals when it's done or an error occurred.
-
elementAt(
int index) → Future<T> -
Returns the value of the
index
th data event of this stream. -
every(
bool test(T element)) → Future<bool> -
Checks whether
test
accepts all elements provided by this stream. -
expand<S>(
Iterable<S> convert(T value)) → Stream<S> -
Creates a new stream from this stream that converts each element into zero or more events.
-
firstWhere(
bool test(T element), { Object defaultValue() }) → Future -
Finds the first element of this stream matching
test
. -
fold<S>(
S initialValue, S combine(S previous, T element)) → Future<S> -
Reduces a sequence of values by repeatedly applying
combine
. -
forEach(
void action(T element)) → Future -
Executes
action
on each data event of the stream. -
handleError(
Function onError, { bool test(error) }) → Stream<T> -
Creates a wrapper Stream that intercepts some errors from this stream.
-
join(
[String separator = "" ]) → Future<String> -
Collects string of data events' string representations.
-
lastWhere(
bool test(T element), { Object defaultValue() }) → Future -
Finds the last element in this stream matching
test
. -
listen(
void onData(T event), { Function onError, void onDone(), bool cancelOnError }) → StreamSubscription<T> -
Adds a subscription to this stream.
-
map<S>(
S convert(T event)) → Stream<S> -
Creates a new stream that converts each element of this stream to a new value using the
convert
function. -
pipe(
StreamConsumer<T> streamConsumer) → Future -
Pipe the events of this stream into
streamConsumer
. -
reduce(
T combine(T previous, T element)) → Future<T> -
Reduces a sequence of values by repeatedly applying
combine
. -
singleWhere(
bool test(T element)) → Future<T> -
Finds the single element in this stream matching
test
. -
skip(
int count) → Stream<T> -
Skips the first
count
data events from this stream. -
skipWhile(
bool test(T element)) → Stream<T> -
Skip data events from this stream while they are matched by
test
. -
take(
int count) → Stream<T> -
Provides at most the first
count
data events of this stream. -
takeWhile(
bool test(T element)) → Stream<T> -
Forwards data events while
test
is successful. -
timeout(
Duration timeLimit, { void onTimeout(EventSink<T> sink) }) → Stream<T> -
Creates a new stream with the same events as this stream.
-
toList(
) → Future<List<T>> -
Collects the data of this stream in a List.
-
toSet(
) → Future<Set<T>> -
Collects the data of this stream in a Set.
-
transform<S>(
StreamTransformer<T, S> streamTransformer) → Stream<S> -
Chains this stream as the input of the provided StreamTransformer.
-
where(
bool test(T event)) → Stream<T> -
Creates a new stream from this stream that discards some data events.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited -
toString(
) → String -
Returns a string representation of this object.
inherited