如何在flutter应用程序的firestore数据库中的文档中添加文档ID [英] How to add document Id in the document in firestore database in flutter application

查看:76
本文介绍了如何在flutter应用程序的firestore数据库中的文档中添加文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在flutter应用程序的firestore集合中添加文档.我不知道如何在文档中添加文档ID.请引导我

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 me

    Firestore.instance.collection("posts1").add({
                    //"id": "currentUser.user.uid",
                    "postTitle": posttitle.text,
                    "postcategory": selectedCategory,
                    "post_id":documentId,
                      })
                      .then((result) =>
                  {print("success")}

推荐答案

使用

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
})

这篇关于如何在flutter应用程序的firestore数据库中的文档中添加文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆