如何在flutter应用程序的firestore数据库中的文档中添加文档ID
问题内容:
我正在使用下面的代码在flutter应用程序的firestore集合中添加文档。我不知道如何在文档中添加文档ID。请指导我
Firestore.instance.collection("posts1").add({
//"id": "currentUser.user.uid",
"postTitle": posttitle.text,
"postcategory": selectedCategory,
"post_id":documentId,
})
.then((result) =>
{print("success")}
问题答案:
使用不带参数的document()首先生成对具有随机ID的文档的引用,然后使用setData()创建它,并将documentID添加为新文档的一部分:
DocumentReference ref = Firestore.instance.collection("posts1").document();
ref.setData({
"post_id": ref.documentID,
// ... add more fields here
})