Home > Back-end >  Firestore how to access subcollection inside docs map (Flutter)
Firestore how to access subcollection inside docs map (Flutter)

Time:03-11

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.

CodePudding user response:

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.

  • Related