我的功能在下面,并且我将错误屏幕快照放在其下。我尝试了很多事情,但找不到优雅而适当的方法来实现这一目标。感谢您的帮助。
**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();
},
);
}
答案 0 :(得分:0)
我发现解决方案也许有人需要。
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());
},
);
}