【问题标题】:How to get current auto generated document ID during posting with cloud firestore in flutter?如何在flutter中使用cloud firestore发布时获取当前自动生成的文档ID?
【发布时间】:2020-04-12 15:21:13
【问题描述】:

我正在使用 firestore 发布位置信息,并在单击按钮时将其数据保存到数据库中。 每个按钮单击它都会生成自动DocumnetID,并希望将此ID同时保存到数据库中。

代码:


await firestore.collection("Table_Name").document().setData({
        "SpotLatLong":new GeoPoint(spotlat, spotlong) ,
        "spotName": placeNameController.text,
        "SpotImage":spotImage,
        "UserID":uid,
        "SpotActivity":spotActivity,
        "SpotDocumentID": firestore.collection("Table_Name").document().documentID.toString()//this line generates Different documentID, which is wrong
      }).then((onValue){
        StyleWidget.showToast(msg: "Created spot to the table");
        Navigator.pop(context);
        isLoading = false;
      });

【问题讨论】:

    标签: database flutter dart google-cloud-firestore


    【解决方案1】:
    String id= await firestore.collection("Table_Name").document().documentID().toString;
    
    await firestore.collection("Table_Name").document(id).setData({
            "SpotLatLong":new GeoPoint(spotlat, spotlong) ,
            "spotName": placeNameController.text,
            "SpotImage":spotImage,
            "UserID":uid,
            "SpotActivity":spotActivity,
            "SpotDocumentID":id //this line generates Different documentID, which is wrong
          }).then((onValue){
            StyleWidget.showToast(msg: "Created spot to the table");
            Navigator.pop(context);
            isLoading = false;
          });
    

    希望对此有所帮助

    【讨论】:

      【解决方案2】:

      必须以编程方式将新文档添加到文档 ID,如下所示,

      // Your Firestore Database Reference
      var tableReference = Firestore.instance.collection("Tables");
      
      // Push a new ID every time like this
      var ID = groupReference.document().documentID;
      
      // Set Data to the new Document ID
      await firestore.collection("Table_Name").**document(ID)**.setData({
              "SpotLatLong":new GeoPoint(spotlat, spotlong) ,
              "spotName": placeNameController.text,
              "SpotImage":spotImage,
              "UserID":uid,
              "SpotActivity":spotActivity,
              "SpotDocumentID": firestore.collection("AddSpot").document().documentID.toString()//this line generates Different documentID, which is wrong
            }).then((onValue){
              StyleWidget.showToast(msg: "Created spot to the table");
              Navigator.pop(context);
              isLoading = false;
            });
      

      【讨论】:

        【解决方案3】:
        var docRef = await firestore.collection("Table_Name").document().setData({
                "SpotLatLong":new GeoPoint(spotlat, spotlong) ,
                "spotName": placeNameController.text,
                "SpotImage":spotImage,
                "UserID":uid,
                "SpotActivity":spotActivity,
                "SpotDocumentID": firestore.collection("AddSpot").document().documentID.toString()//this line generates Different documentID, which is wrong
              }).then((onValue){
                StyleWidget.showToast(msg: "Created spot to the table");
                Navigator.pop(context);
                isLoading = false;
              });
        

        通过这种方式获取文档参考

        docRef.documentID 
        

        【讨论】:

        • 看起来 setData() 返回 void 而不是文档参考
        猜你喜欢
        • 1970-01-01
        • 2020-11-15
        • 2020-08-18
        • 2019-05-05
        • 2021-07-31
        • 1970-01-01
        • 2020-12-05
        • 2022-07-22
        • 2021-07-20
        相关资源
        最近更新 更多