StreamController<T> class
Null safety
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.
Example:
final streamController = StreamController(
onPause: () => print('Paused'),
onResume: () => print('Resumed'),
onCancel: () => print('Cancelled'),
onListen: () => print('Listens'),
);
streamController.stream.listen(
(event) => print('Event: $event'),
onDone: () => print('Done'),
onError: (error) => print(error),
);
To check whether there is a subscriber on the stream, use hasListener.
var hasListener = streamController.hasListener; // true
To send data events to the stream, use add or addStream.
streamController.add(999);
final stream = Stream<int>.periodic(
const Duration(milliseconds: 200), (count) => count * count).take(4);
await streamController.addStream(stream);
To send an error event to the stream, use addError or addStream.
streamController.addError(Exception('Issue 101'));
await streamController.addStream(Stream.error(Exception('Issue 404')));
To check whether the stream is closed, use isClosed.
var isClosed = streamController.isClosed; // false
To close the stream, use close.
await streamController.close();
isClosed = streamController.isClosed; // true
- Implemented types
-
- StreamSink<
T>
- StreamSink<
- Implementers
Constructors
-
StreamController({void onListen()?, void onPause()?, void onResume()?, FutureOr<
void> onCancel()?, bool sync = false}) -
A controller with a stream that supports only one single subscriber.
factory
- StreamController.broadcast({void onListen()?, void onCancel()?, bool sync = false})
-
A controller where stream can be listened to more than once.
factory
Properties
- done → Future
-
A future which is completed when the stream controller is done
sending events.
read-only, override
- hashCode → int
-
The hash code for this object.
read-only, inherited
- hasListener → bool
-
Whether there is a subscriber on the Stream.
read-only
- isClosed → bool
-
Whether the stream controller is closed for adding more events.
read-only
- isPaused → bool
-
Whether the subscription would need to buffer events.
read-only
-
onCancel
↔ (FutureOr<
void> Function?()?) -
The callback which is called when the stream is canceled.
read / write
- onListen ↔ (void Function?()?)
-
The callback which is called when the stream is listened to.
read / write
- onPause ↔ (void Function?()?)
-
The callback which is called when the stream is paused.
read / write
- onResume ↔ (void Function?()?)
-
The callback which is called when the stream is resumed.
read / write
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
-
sink
→ StreamSink<
T> -
Returns a view of this object that only exposes the StreamSink interface.
read-only
-
stream
→ Stream<
T> -
The stream that this controller is controlling.
read-only
Methods
-
add(
T event) → void -
Sends a data
event
.override -
addError(
Object error, [StackTrace? stackTrace]) → void -
Sends or enqueues an error event.
override
-
addStream(
Stream< T> source, {bool? cancelOnError}) → Future -
Receives events from
source
and puts them into this controller's stream.override -
close(
) → Future -
Closes the stream.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited