Reputation: 23
I structured my firestore database as follow:
to display the data in flutter i use streambuilder as follow:
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('myCollection').snapshots(),
builder: (context, snapshot) {
return Container(
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) =>
_buildListItem(context, snapshot.data.documents[index]),
),
);
}),
Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
return ListTile(
title: Row(
children: <Widget>[
Text(document.documentID),
Spacer(),
Text(document['allocatedSource'].toString()),
],
),
how do I access the subcollection like this: subtitle: Text(document.documentID.collection('post').document).
I can access that if i declare the stream as follow:
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('myCollection')
.document(documentId)
.collection('post')
.snapshots(),...
but then I will lose the access to the documentID from the collection. Should I declare 2 stream or nest the streambuilder?
Upvotes: 0
Views: 4327
Reputation: 317352
Subcollection data are not included in document snapshots. Firestore queries are shallow, and only consider documents within a single collection.
If you want subcollection data, you'll have to make new query using the subcollection name that you know ahead of time, as you have seen.
And, I will point out that you haven't "lost" access to documentId
in the second query. It's still in memory, and you can use it at will. You can also walk up the parent document and collections to get the ID you're looking for.
Upvotes: 2