StreamController abstract class

A controller with the stream it controls.

This controller allows sending data, error and done events on its stream. This class can be used to create a simple stream that others can listen on, and to push events to that stream.

It's possible to check whether the stream is paused or not, and whether it has subscribers or not, as well as getting a callback when either of these change.

If the stream starts or stops having listeners (first listener subscribing, last listener unsubscribing), the onSubscriptionStateChange callback is notified as soon as possible. If the subscription stat changes during an event firing or a callback being executed, the change will not be reported until the current event or callback has finished. If the pause state has also changed during an event or callback, only the subscription state callback is notified.

If the subscriber state has not changed, but the pause state has, the onPauseStateChange callback is notified as soon as possible, after firing a current event or completing another callback. This happens if the stream is not paused, and a listener pauses it, or if the stream has been resumed from pause and has no pending events. If the listeners resume a paused stream while it still has queued events, the controller will still consider the stream paused until all queued events have been dispatched.

Whether to invoke a callback depends only on the state before and after a stream action, for example firing an event. If the state changes multiple times during the action, and then ends up in the same state as before, no callback is performed.

If listeners are added after the stream has completed (sent a "done" event), the listeners will be sent a "done" event eventually, but they won't affect the stream at all, and won't trigger callbacks. From the controller's point of view, the stream is completely inert when has completed.

Implements
Implemented by

Constructors

StreamController({void onListen(), void onPause(), void onResume(), dynamic onCancel(), bool sync: false })
factory

A controller with a stream that supports only one single subscriber.

StreamController.broadcast({void onListen(), void onCancel(), bool sync: false })
factory

A controller where stream can be listened to more than once.

Properties

hasListenerbool
read-only

Whether there is a subscriber on the Stream.

isClosedbool
read-only

Whether the stream controller is closed for adding more events.

isPausedbool
read-only

Whether the subscription would need to buffer events.

onCancelControllerCancelCallback
read / write

The callback which is called when the stream is canceled.

onListenControllerCallback
read / write

The callback which is called when the stream is listened to.

onPauseControllerCallback
read / write

The callback which is called when the stream is paused.

onResumeControllerCallback
read / write

The callback which is called when the stream is resumed.

sinkStreamSink<T>
read-only

Returns a view of this object that only exposes the StreamSink interface.

streamStream<T>
read-only

The stream that this controller is controlling.

doneFuture
read-only, inherited

Return a future which is completed when the StreamSink is finished.

hashCodeint
read-only, inherited

The hash code for this object.

runtimeTypeType
read-only, inherited

A representation of the runtime type of the object.

Operators

operator ==(other) → bool
inherited

The equality operator.

Methods

add(T event) → void

Sends a data event.

addError(Object error, [ StackTrace stackTrace ]) → void

Sends or enqueues an error event.

addStream(Stream<T> source, { bool cancelOnError: true }) → Future

Receives events from source and puts them into this controller's stream.

close() → Future

Closes the stream.

noSuchMethod(Invocation invocation) → dynamic
inherited

Invoked when a non-existent method or property is accessed.

toString() → String
inherited

Returns a string representation of this object.

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-async/StreamController-class.html