W3cubDocs

/Dart 2

toSet method

Future<Set<T>> toSet ()

Collects the data of this stream in a Set.

The returned set is the same type as returned by new Set<T>(). If another type of set is needed, either use forEach to add each element to the set, or use toList().then((list) => new SomeOtherSet.from(list)) to create the set.

Implementation

Future<Set<T>> toSet() {
  Set<T> result = new Set<T>();
  _Future<Set<T>> future = new _Future<Set<T>>();
  this.listen(
      (T data) {
        result.add(data);
      },
      onError: future._completeError,
      onDone: () {
        future._complete(result);
      },
      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/toSet.html