【发布时间】:2019-12-03 20:41:35
【问题描述】:
问题已更新
如何查询 2 个集合?我有一个愿望清单集合,其中每个愿望清单文档如下所示:
documentID: "some-wishlist-id",
modified: "1564061309920",
productId: "2tqXaLUDy1IjxLafIu9O",
userId: "0uM7Dt286JYK6q8iLFyF4tG9cK53"
还有一个产品集合,其中每个产品文档看起来像这样。愿望清单集合中的 productId 将是产品集合中的 documentID:
documentID: "2tqXaLUDy1IjxLafIu9O",
dateCreated: "1563820643577",
description: "This is a description for Product 9",
images: ["some_image.jpg"],
isNegotiable: true,
isSold: false,
rentPrice: 200,
sellPrice: 410,
title: "Product 9",
totalWishLists: 0,
userId: "0uM7Dt286JYK6q8iLFyF4tG9cK53"
注意:为了清楚起见,愿望清单查询应返回我需要迭代以检索产品的文档列表。
不确定在这种情况下我是否需要使用流或期货,但这是我目前所拥有的:
Future<Product> getProduct(String documentId) {
return Firestore.instance
.collection(APIPath.products())
.document(documentId)
.get()
.then((DocumentSnapshot ds) => Product.fromMap(ds.data));
}
Query getWishListByUser(userId) {
return Firestore.instance
.collection(APIPath.wishlists())
.where('userId', isEqualTo: userId);
}
Future<List<Product>> getProductsWishList(userId) async {
List<Product> _products = [];
await getWishListByUser(userId)
.snapshots()
.forEach((QuerySnapshot snapshot) {
snapshot.documents.forEach((DocumentSnapshot snapshot) async {
Map<String, dynamic> map = Map.from(snapshot.data);
map.addAll({
'documentID': snapshot.documentID,
});
WishList wishList = WishList.fromMap(map);
await getProduct(wishList.productId).then((Product product) {
print(product.title); // This is printing
_products.add(product);
});
});
});
// This is not printing
print(_products);
return _products;
}
谢谢
【问题讨论】:
-
您想在列表中呈现该数据吗?是指产品中的一些字段和愿望清单中的一些字段?
-
@MuhammadNoman 我只想退回愿望清单中的产品。所以愿望清单中没有任何内容。
-
你有那个愿望清单的wishlistId?
-
是的。我会将 documentID 添加到原始问题中。
-
查看以下答案
标签: firebase flutter dart google-cloud-firestore