我要发布一个带有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;
});
答案 0 :(得分:2)
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;
});
希望对此有所帮助
答案 1 :(得分:1)
要以编程方式将新文档添加到文档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;
});
答案 2 :(得分:1)
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