close method Null safety

Future<void> close()

Closes all transformed streams.

Returns a future that completes when all inner subscriptions' StreamSubscription.cancel futures have completed. Note that a stream's subscription won't be canceled until the transformed stream has a listener.

If a transformed stream is listened to after close is called, the original stream will be listened to and then the subscription immediately canceled. If that cancellation throws an error, it will be silently ignored.

Implementation

Future<void> close() => _closeFuture ??= () {
      var futures = [
        for (var subscription in _subscriptions) subscription.cancel()
      ];
      _subscriptions.clear();

      var controllers = _controllers.toList();
      _controllers.clear();
      scheduleMicrotask(() {
        for (var controller in controllers) {
          scheduleMicrotask(controller.close);
        }
      });

      return Future.wait(futures, eagerError: true);
    }();