在地图上的溪流中等待



我有以下流:

Stream<List<Product>> products() {
//Get Products from Cloud Firestore
return productCollection.snapshots().map((snapshot) {
return snapshot.documents.map((document) {
//Get image metadata of each product from Firebase Storage
Future<StorageMetadata> _metadata = imageRef
.child('${document.documentID}/${document.data['mainImage']['name']}')
.getMetadata()
.catchError((onError) => print('Error: $onError'));
//After getting metadata, create product objects with data gathered above
return Product.fromEntity(ProductEntity.fromSnapshot(
document, ProductImageEntity.fromMetadata(_metadata)));
}).toList();
});
}

在从Firebase Storage检索元数据后,我需要返回产品对象。我是异步编程的新手,在不将流返回类型更改为Future的情况下,我很难做到这一点。如何做到这一点?

通过执行以下操作完成:

Stream<List<Product>> products() {
//Get Products from Cloud Firestore
return productCollection.snapshots().asyncMap((snapshot) {
return Future.wait(snapshot.documents.map((document) async {
//Get image metadata of each product from Firebase Storage
StorageMetadata _metadata = await imageRef
.child(
'${document.documentID}/${document.data['mainImage']['name']}')
.getMetadata()
.catchError((onError) => print('Error: $onError'));
//After getting metadata, create product objects with data gathered above
return Product.fromEntity(ProductEntity.fromSnapshot(
document,
ProductImageEntity.fromMetadata(_metadata),
));
}).toList());
});
}

最新更新