Hanna
Hanna

Reputation: 153

Firestore / Flutter - How can I get document Id?

this is my code

Future _save() async {

final StorageReference storageRef = FirebaseStorage.instance.ref().child('product/'+nameController.text+'.jpg');
final StorageUploadTask task = storageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await task.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
StorageMetadata created = await taskSnapshot.ref.getMetadata();

Firestore.instance.collection('product').document()
    .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': //I want to set document Id here //
});
}

how can I get this random generated document's ID? Thank you for your help

Upvotes: 15

Views: 21052

Answers (5)

Rahul Kushwaha
Rahul Kushwaha

Reputation: 6722

Fetch the partgiculer documentId using value.Id.

 CollectionReference users = FirebaseFirestore.instance.collection('candidates');
      
      Future<void> registerUser() {
     // Call the user's CollectionReference to add a new user
         return users.add({
          'name': enteredTextName, // John Doe
          'email': enteredTextEmail, // Stokes and Sons
          'profile': dropdownValue ,//
          'date': selectedDate.toLocal().toString() ,//// 42
        })
            .then((value) =>(showDialogNew(value.id)))
            .catchError((error) => print("Failed to add user: $error"));
      }

Upvotes: 0

Matthias
Matthias

Reputation: 4163

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.

Upvotes: 5

Frank Tz
Frank Tz

Reputation: 613

You can get the ID by calling your ref.documentID

Upvotes: 1

Cenk YAGMUR
Cenk YAGMUR

Reputation: 3451

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

Upvotes: 7

diegoveloper
diegoveloper

Reputation: 103421

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 

Upvotes: 15

Related Questions