简体   繁体   English

从 Firestore 子集合 flutter 获取所有数据

[英]get all data from a firestore subcollection flutter

I want to get all the data in a firestore subcollection.我想获取 firestore 子集合中的所有数据。

This is how my firestore collection looks like:这就是我的 Firestore 收藏的样子:

火库收藏

The collection Products has a product with a field productName.集合 Products 有一个带有字段 productName 的产品。 I

I can display the list name by accesing the snapshot data我可以通过访问快照数据来显示列表名称

CloudShoppingListsCart.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot, this.products)
  : shoppingListId = snapshot.id,
    name = snapshot.data()['name'] as String;

but is there a way to access the collection products and his fields like I did in snapshot.data() ?但是有没有办法像我在snapshot.data()中那样访问集合products和他的字段?

The method where I call fromSnapshot is this:我调用fromSnapshot的方法是这样的:

Stream<Iterable<CloudShoppingListsCart>> getShoppingCartListDetails({required String shoppingCartListId}){

final shoppingList = FirebaseFirestore.instance.collection(shoppingListCollectionName)
    .doc(shoppingCartListId).collection(shoppingListCartCollectionName).snapshots().map((event) =>
    event.docs
        .map((doc) => CloudShoppingListsCart.fromSnapshot(doc, [])));

return shoppingList;

I want to get the product name for displaying it in a ListTile like this我想获取产品名称以便在这样的 ListTile 中显示它

return ListTile(
  leading: const Icon(Icons.list),
  trailing: const Text(
     "GFG",
     style: TextStyle(color: Colors.green, fontSize: 15),),

  title: Text('List item $productsName'));

I am already displaying the list name in a column.我已经在列中显示列表名称。

Thanks in advance.提前致谢。

As Peter commented: read operations on Firestore are shallow.正如 Peter 评论的那样:Firestore 上的读取操作很浅。 A document snapshot from the parent collection, does not contain snapshots from its subcollections.来自父集合的文档快照不包含来自其子集合的快照。

This means that you will have to execute an additional read operation to get the documents from the products subcollection.这意味着您必须执行额外的读取操作才能从products子集合中获取文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM