fluttergoogle-cloud-firestore

How to get firestore document ID from document snapshot?


QuerySnapshot querySnapshot = await _collectionRef   
                                  .limit(1)
                                  .orderBy('date')                                                                    
                                  .getDocuments();
var list = querySnapshot.documents;

querySnapshot.documents.map((document) { 
    print('Document : $document'); // not printing anything.
  });

if(list.length > 0) {
   print('ID : ${list[0].documentID}'); // not working
}

This is the exact code i tried... What is the mistake in the code?


Solution

  • According to the API documentation, DocumentSnapshot has a documentId property, so I would expect this to work:

    querySnapshot.documents[0].documentID