Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:async
  3. StreamController<T> class
StreamController
description

StreamController<T> classabstractinterface

A controller with the stream it controls.

This controller allows sending data, error and done events onitsstream.

This class can be used to create a simple stream that otherscan listen on, and to push events to that stream.

It's possible to check whether the stream is paused or not, and whetherit has subscribers or not, as well as getting a callback when either ofthese 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, usehasListener.

var hasListener = streamController.hasListener; // true

To send data events to the stream, useadd oraddStream.

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, useaddError oraddStream.

streamController.addError(Exception('Issue 101'));await streamController.addStream(Stream.error(Exception('Issue 404')));

To check whether the stream is closed, useisClosed.

var isClosed = streamController.isClosed; // false

To close the stream, useclose.

await streamController.close();isClosed = streamController.isClosed; // true
Implemented types
Implementers

Constructors

StreamController({voidonListen()?,voidonPause()?,voidonResume()?,FutureOr<void>onCancel()?,boolsync =false})
A controller with astream that supports only one single subscriber.
factory
StreamController.broadcast({voidonListen()?,voidonCancel()?,boolsync =false})
A controller wherestream can be listened to more than once.
factory

Properties

doneFuture
A future which is completed when the stream controller is donesending events.
no setteroverride
hashCodeint
The hash code for this object.
no setterinherited
hasListenerbool
Whether there is a subscriber on theStream.
no setter
isClosedbool
Whether the stream controller is closed for adding more events.
no setter
isPausedbool
Whether the subscription would need to buffer events.
no setter
onCancelFutureOr<void> Function()?
The callback which is called when the stream is canceled.
getter/setter pair
onListen↔ void Function()?
The callback which is called when the stream is listened to.
getter/setter pair
onPause↔ void Function()?
The callback which is called when the stream is paused.
getter/setter pair
onResume↔ void Function()?
The callback which is called when the stream is resumed.
getter/setter pair
runtimeTypeType
A representation of the runtime type of the object.
no setterinherited
sinkStreamSink<T>
Returns a view of this object that only exposes theStreamSink interface.
no setter
streamStream<T>
The stream that this controller is controlling.
no setter

Methods

add(Tevent)→ void
Sends a dataevent.
override
addError(Objecterror, [StackTrace?stackTrace])→ void
Sends or enqueues an error event.
override
addStream(Stream<T>source, {bool?cancelOnError})Future
Receives events fromsource and puts them into this controller's stream.
override
close()Future
Closes the stream.
override
noSuchMethod(Invocationinvocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString()String
A string representation of this object.
inherited

Operators

operator ==(Objectother)bool
The equality operator.
inherited
  1. Dart
  2. dart:async
  3. StreamController<T> class
dart:async library

[8]ページ先頭

©2009-2025 Movatter.jp