manageStreamController method

  1. @mustCallSuper
  2. @override
void manageStreamController (StreamController controller)
@mustCallSuper, override

Automatically cancel a stream controller when this object is disposed.

The parameter may not be null.


void manageStreamController(StreamController controller) {
  _throwOnInvalidCall('manageStreamController', 'controller', controller);
  // If a single-subscription stream has a subscription and that
  // subscription is subsequently canceled, the `done` future will
  // complete, but there is no other way for us to tell that this
  // is what has happened. If we then listen to the stream (since
  // closing a stream that was never listened to never completes) we'll
  // get an exception. This workaround allows us to "know" when a
  // subscription has been canceled so we don't bother trying to
  // listen to the stream before closing it.

  bool isDone = false;

  var disposable = new ManagedDisposer(() {
    if (!controller.hasListener && !controller.isClosed && !isDone) { {});
    return controller.close();

  controller.done.then((_) {
    isDone = true;
    // ignore: deprecated_member_use
    if (!isDisposedOrDisposing) {