W3cubDocs

/Dart 2

length property

Future<int> length

The number of elements in this stream.

Waits for all elements of this stream. When the stream ends, the returned future is completed with the number of elements.

If the stream contains an error, the returned future is completed with that error, and processing stops.

This operation listens to the stream, and a non-broadcast stream cannot be reused after finding its length.

Implementation

Future<int> get length {
  _Future<int> future = new _Future<int>();
  int count = 0;
  this.listen(
      (_) {
        count++;
      },
      onError: future._completeError,
      onDone: () {
        future._complete(count);
      },
      cancelOnError: true);
  return future;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-async/Stream/length.html