小编Néo*_*igi的帖子

飞镖-是否有可能改变future.forEach。到地图?

所以基本上我有这段工作代码:

List<User> users = List();
await Future.forEach(querySnapshot.documents, (doc) async {
  final snapshot = await doc['user'].get();
  users.add(User(id: snapshot["id"], name: snapshot["mail"]));
});

return users;
Run Code Online (Sandbox Code Playgroud)

它可以正常工作,并且完全满足我的需求,但是我想知道是否有办法将其更改为地图,例如:

return querySnapshot.documents.map((doc) async {
  final snapshot = await doc['user'].get();
  User(id: snapshot["id"], name: snapshot["mail"]);
}).toList{growable: true};
Run Code Online (Sandbox Code Playgroud)

我这样做的问题是它说:无法将List <Future <Null >>类型的值赋给List <User>类型的变量。

所以我想知道是否有可能或唯一的方法是使用Future.forEach

foreach async-await dart dart-async

3
推荐指数
1
解决办法
796
查看次数

标签 统计

async-await ×1

dart ×1

dart-async ×1

foreach ×1