Dart中将Iterable转换为Stream的最佳/最短方法是什么?

Set*_*add 9 dart dart-async

我有一个Iterable,我想将它转换为Stream.什么是最有效/最短的代码量?

例如:

Future convert(thing) {
  return someAsyncOperation(thing);
}

Stream doStuff(Iterable things) {
  return things
    .map((t) async => await convert(t)) // this isn't exactly what I want
                                        // because this returns the Future
                                        // and not the value
    .where((value) => value != null)
    .toStream(); // this doesn't exist...
}
Run Code Online (Sandbox Code Playgroud)

注意:iterable.toStream()不存在,但我想要这样的东西.:)

w.b*_*ian 7

这是一个简单的例子:

var data = [1,2,3,4,5]; // some sample data
var stream = new Stream.fromIterable(data);
Run Code Online (Sandbox Code Playgroud)

使用你的代码:

Future convert(thing) {
  return someAsyncOperation(thing);
}

Stream doStuff(Iterable things) {
  return new Stream.fromIterable(things
    .map((t) async => await convert(t))
    .where((value) => value != null));
}
Run Code Online (Sandbox Code Playgroud)