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