database - 如何在 flutter 朔迷离的Cloud Firestore中获取当前自动生成的文档ID?

标签 database flutter dart google-cloud-firestore

我要发布一个带有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;
      });

最佳答案

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

希望可以帮助这个

关于database - 如何在 flutter 朔迷离的Cloud Firestore中获取当前自动生成的文档ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59419699/

相关文章:

database - 如何使用 Cloud Firestore 计算集合中文档的数量

database - 支持 GUID id 列的内存数据库中是否支持 Hibernate?

sql - 在 Postgresql 中加入最大记录

sql - 使用用户名列而不是 id?

http - Flutter Dio 发布一个带有数组的对象

cookies - AngularDart 的Cors问题-未设置Cookie

android - 警告 : [options] source value 7 is obsolete and will be removed in a future release

flutter - 如何知道小部件在视口(viewport)中是否可见?

date - 如何在 DART(Flutter)中舍入最接近 30 分钟间隔的日期时间?

flutter - 如何在模态底部工作表中制作 CheckboxListTile?