简体   繁体   English

如何使用 flutter 从 Firestore 获取子集合?

[英]How do I fetch sub collection from firestore using flutter?

I wanna fetch the data inside the Guests like guestName.我想获取来宾名称中的数据。 The code below I used it to fetch collection and it worked, but I have no idea how to fetch subcollection.下面的代码我用它来获取集合并且它有效,但我不知道如何获取子集合。 I been trying but it's not working.我一直在尝试,但它不起作用。

    body: CustomScrollView(
slivers: [
SliverPersistentHeader(pinned: true, delegate: SearchBoxDelegate()),
StreamBuilder<QuerySnapshot>(

//I used this to fetch collection but it doesn't work with subcollection
stream: Firestore.instance
.collection("Guests")
.orderBy("Date", descending: true).snapshots(),
builder: (context, dataSnapshot) {
return !dataSnapshot.hasData
? SliverToBoxAdapter(
    child: Center(
      child: circularProgress(),
    ),
  )
: SliverStaggeredGrid.countBuilder(
    crossAxisCount: 1,
    staggeredTileBuilder: (c) => StaggeredTile.fit(1),
    itemBuilder: (context, index) {
      ItemModel model = ItemModel.fromJson(
          dataSnapshot.data.documents[index].data);
      return sourceInfo(model, context);
    },
    itemCount: dataSnapshot.data.documents.length,
  );
},
),
],
),

Container(
child: Row(
  mainAxisSize: MainAxisSize.max,
  children: [
    Expanded(
      child: Text(
      // this guestName i wanna fetch
        model.guestName,
        style: TextStyle(
            color: Colors.black, fontSize: 14.0),
      ),
    ),
  ],
),
),

To get a CollectionReference for a subcollection, you first need to have a DocumentReference object to its parent document, and then you can call collection() on that.要获取子集合的CollectionReference ,您首先需要为其父文档提供DocumentReference object ,然后您可以对其调用collection()

In your builder you have dataSnapshot.data.documents[index] , which gives you a DocumentSnapshot object , from which you can get a DocumentReference by calling reference on it.在您的构建器中,您有dataSnapshot.data.documents[index] ,它为您提供了DocumentSnapshot object ,您可以从中通过调用reference来获取DocumentReference

So combined that'd be something like this in your builder:所以结合起来在你的构建器中会是这样的:

dataSnapshot.data.documents[index].reference.collection("Guests")

I recommend always keeping the reference documentation handy that I linked above, as it's the easiest way to find this type of API path.我建议始终将上面链接的参考文档放在手边,因为这是找到这种类型的 API 路径的最简单方法。

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

相关问题 如何将子集合转换为包含 flutter 和 firestore 的列表? - How to convert a sub collection to list with flutter and firestore? 如何从 firestore 集合中获取文档列表 - How do you fetch a list of documents from a firestore collection 如何根据 Flutter 中子集合的值过滤数据 - How do I filter data depending on the values from Sub Collection in Flutter 如何从 flutter 中的云 firestore 获取数据? - How to fetch data from cloud firestore in flutter? 如何按firebase中的子集合中的字段排序flutter中的云Firestore - How to sort by a field in a sub-collection in firebase cloud firestore in flutter 如何在 React 中使用 Map function 渲染 Firestore 集合? - How do I render Firestore collection using the Map function in React? 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript? 如何从 Firebase 的集合中的 Firestore 文档中获取单个字段? - How to fetch a single Field from a Firestore document in a collection from Firebase? 如何通过 Firestore 更新 Flutter 评论子集中的用户名和图片 - How to also update user's name and image in comments sub-collection in Flutter via Firestore 如何使用本机反应将子集合添加到 firestore 中的文档 - How to add a sub collection to a document in firestore using react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM