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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.