[英]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.