Firestore / Flutter - How can I get document Id?

Since v0.14.0 of the cloud_firestore package:

DEPRECATED: documentID has been deprecated in favor of id.

so instead of

ref.documentId

use

ref.id

to retriev the random generated document id.


After collection you can add a document and receive the DocumentReference .

  final docRef = await Firestore.instance.collection('product').add({
    'name': nameController.text,
    'price': int.tryParse(priceController.text),
    'description': descriptionController.text,
    'creator': widget.user.uid,
    'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
    'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
    'url': downloadUrl,
  });

Now you can get the document ID:

 docRef.documentID 

you do it like that

DocumentReference documentReference = Firestore.instance.collection('product').document();    
documentReference.setData({
        'name': nameController.text,
        'price': int.tryParse(priceController.text),
        'description': descriptionController.text,
        'creator': widget.user.uid,
        'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
        'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
        'url': downloadUrl,
        'id': documentReference.documentID
      });

Document ID

documentReference.documentID