'Firestore how to access subcollection inside docs map (Flutter)

I tried to access the subcollection 'match' using docs map.

firestore

final firestore = FirebaseFirestore.instance.collection('time').get();

firestore.docs.map((e) {
   DateTime dt = e.data()['time'];

   e.collection('match');     // How to access its subcollection
})

How can I access the subcollection 'match' on each document and at the same time accessing the 'time' field.



Solution 1:[1]

Your firestore variable is a Future<QuerySnapshot> object, and you're missing an await or then to wait for the future to resolve:

final firestore = FirebaseFirestore.instance.collection('time').get();
final snapshot = await firestore;

You can then access a subcollection of each time document with:

snapshot.docs.map((doc) {
   ...

   var matchCollection = doc.reference.collection('match');
})

You might also want to look at using a collection group query to read/query all match collections in one go.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen