Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 640
agriz Avatar asked Mar 12 '19 03:03

agriz


2 Answers

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

querySnapshot.documents[0].documentID
like image 199
Doug Stevenson Avatar answered Sep 21 '22 12:09

Doug Stevenson


product.id

documentID is deprecated and shouldn't be used. Deprecated in favor of .id. Try replacing the use of the deprecated member with the replacement.

like image 22
Aravin Avatar answered Sep 20 '22 12:09

Aravin