I am using below code to add document in firestore collection in a flutter application. I am not getting an idea that on how to add the document id in the document. Please guide
DocumentReference docRef = await Firestore.instance.collection("products").add({
'description': product.description,
'imageUrl': product.imageUrl,
'price': product.price,
});
final newProduct = Product(
title: product.title,
description: product.description,
price: product.price,
imageUrl: product.imageUrl,
id:docRef.documentID,
);
_items.add(newProduct);
Use document() with no arguments to first generate a reference to a document with a random ID, then use setData() to create it and add the documentID as part of the new document:
DocumentReference ref = Firestore.instance.collection("posts1").document();
ref.setData({
"post_id": ref.documentID,
// ... add more fields here
})