How to add document Id in the document in firestore database in flutter application

前端 未结 2 511
暖寄归人
暖寄归人 2021-01-28 21:13

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

2条回答
  •  一整个雨季
    2021-01-28 21:54

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

提交回复
热议问题