Dart - 是否可以更改 future.forEach。到地图?
Dart - is it possible to change a future.forEach. to a map?
所以基本上我有这段工作代码:
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;
它工作正常并且完全符合我的需要,但我想知道是否有办法以某种方式将其更改为地图,例如:
return querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
User(id: snapshot["id"], name: snapshot["mail"]);
}).toList{growable: true};
当我这样做时出现的问题是它说:List> 类型的值不能分配给 List 类型的变量。
所以我想知道这是否可能,或者唯一的方法是 Future.forEach
要将 Future 列表变成单个 Future,请使用 dart:async
中的 Future.wait
。 (也不要忘记 return 用户对象)。
final List<User> = await Future.wait(querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
return User(id: snapshot["id"], name: snapshot["mail"]);
}));
所以基本上我有这段工作代码:
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;
它工作正常并且完全符合我的需要,但我想知道是否有办法以某种方式将其更改为地图,例如:
return querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
User(id: snapshot["id"], name: snapshot["mail"]);
}).toList{growable: true};
当我这样做时出现的问题是它说:List
所以我想知道这是否可能,或者唯一的方法是 Future.forEach
要将 Future 列表变成单个 Future,请使用 dart:async
中的 Future.wait
。 (也不要忘记 return 用户对象)。
final List<User> = await Future.wait(querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
return User(id: snapshot["id"], name: snapshot["mail"]);
}));