listenToStream<T> method

  1. @mustCallSuper
  2. @override
StreamSubscription<T> listenToStream <T>(Stream<T> stream, void onData(T event), { Function onError, void onDone(), bool cancelOnError })
@mustCallSuper, override

Returns a StreamSubscription which handles events from the stream using the provided onData, onError and onDone handlers.

Consult documentation for Stream.listen for more info.

If the returned StreamSubscription is cancelled manually (i.e. canceled before disposal of the parent object) Disposable will clean up the internal reference allowing the subscription to be garbage collected.

Neither parameter may be null.


StreamSubscription<T> listenToStream<T>(
    Stream<T> stream, void onData(T event),
    {Function onError, void onDone(), bool cancelOnError}) {
  _throwOnInvalidCall2('listenToStream', 'stream', 'onData', stream, onData);
  var managedStreamSubscription = new ManagedStreamSubscription(
      stream, onData,
      onError: onError, onDone: onDone, cancelOnError: cancelOnError);

  var disposable = new ManagedDisposer(() {
    return managedStreamSubscription.cancel();


  managedStreamSubscription.didComplete.then((_) {
    // ignore: deprecated_member_use
    if (!isDisposedOrDisposing) {

  return managedStreamSubscription;