[英]How can i wait for Future in map function?
My function is below and i put the error screen shot under it.我的功能在下面,我把错误屏幕截图放在它下面。 i tried lots of thing but could not find elegant and proper way to achieve this.我尝试了很多东西,但找不到优雅和正确的方法来实现这一点。 Thanks for your help.谢谢你的帮助。
**It works**
Future<List<UserLike>> getUserLikedPosts() async {
return userCollection
.document(_currentUserId)
.collection(collection_like)
.limit(POST_LOAD_LIMIT)
.getDocuments()
.then(
(snapshot) {
if (snapshot == null) return null;
return snapshot.documents
.map((userImage) =>
UserLike.fromEntity(UserLikeEntity.fromSnapshot(userImage)))
.toList();
},
);
}
**It does not work**
Future<List<UserLike>> getUserLikedPosts() async {
return userCollection
.document(_currentUserId)
.collection(collection_like)
.limit(POST_LOAD_LIMIT)
.getDocuments()
.then(
(snapshot) {
if (snapshot == null) return null;
return snapshot.documents.map((userImage) async {
final postEntity =
UserLike.fromEntity(UserLikeEntity.fromSnapshot(userImage));
// NEED TO GET DOWNLOAD URL AND UPDATE MY OBJECT
final downloadUrl = await FirebaseStorage.instance
.ref()
.child("user/${post.userId}/post/${post.file}")
.getDownloadURL();
return postEntity.copyWith(downloadUrl: downloadUrl);
}).toList();
},
);
}
I found the solution maybe some one need it.我找到了解决方案,也许有人需要它。
Future<List<UserLike>> getUserLikedPosts() async {
return userCollection
.document(_currentUserId)
.collection(collection_like)
.limit(POST_LOAD_LIMIT)
.getDocuments()
.then(
(snapshot) async {
if (snapshot == null) return null;
return await Future.wait(snapshot.documents.map((userImage) async {
final post = UserLike.fromEntity(UserLikeEntity.fromSnapshot(userImage));
final url = await _storageRef.child("user/${post.userId}/post/${post.file}").getDownloadURL();
return post.copyWith(downloadUrl: url);
}).toList());
},
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.