Firebase 我可以在stream.map()中使用异步函数吗?

Firebase 我可以在stream.map()中使用异步函数吗?,firebase,flutter,asynchronous,dart,stream,Firebase,Flutter,Asynchronous,Dart,Stream,是否可以将异步函数传递到stream.map()函数中 final CollectionReference myCollection = FirebaseFirestore.instance.collection('foo'); Future<Stream<MyModel>> get myStream async { // incorrect code, but I want to do something like this return a

是否可以将异步函数传递到stream.map()函数中

final CollectionReference myCollection =
    FirebaseFirestore.instance.collection('foo');


Future<Stream<MyModel>> get myStream async {
    // incorrect code, but I want to do something like this
    return await myCollection.snapshots().map(_mappingFunc);
}

Future<MyModel> _mappingFunc(QuerySnapshot snapshot) async {
    // some async code
}

最终收集参考myCollection=
FirebaseFirestore.instance.collection('foo');
未来获取myStream异步{
//不正确的代码,但我想这样做
返回wait wait myCollection.snapshots().map(_mappingFunc);
}
Future\u mappingFunc(QuerySnapshot快照)异步{
//一些异步代码
}

这是不可能的,因为
地图的声明如下:

Stream<S> map <S>(
      S convert(
          T event
        )
     )
流图(
S转换(
T事件
)
)
map
采用
convert
函数,其类型为
S
,基本上就是所使用的
流的类型

您可以使用
asyncMap()
方法:

您可以使用执行输出到
流的映射
,也可以包装函数并等待结果,使用异步函数List.map获得
列表
结果:

myCollection.snapshots().map((e)async=>await\u mappingFunc(e));
另一个选项是使用
async*
方法来实现
输出:

Stream获取myStream异步*{
for(myCollection.snapshots()中的最终快照){
产量等待(快照);;
}
}