简体   繁体   English

Flutter / Dart:将数据保存在Firestore中的不同文档中

[英]Flutter/Dart: save data in different documents in firestore

I am trying to add a data to a firestore with following code. 我正在尝试使用以下代码将数据添加到Firestore中。
I select several features such as food, field, date, time, and location. 我选择了几个功能,例如食物,田地,日期,时间和位置。 When I press a button Future _addAll function is called and data above are saved into the firestore. 当我按下按钮时,会调用Future _addAll函数,并将上面的数据保存到Future _addAll

After saving the page moves to main page. 保存后,页面移至主页。 The problem starts from here. 问题从这里开始。 When I select different data and try to save it to firestore with new documentID, it simply overrides to the previous document unless I refresh the page. 当我选择其他数据并尝试使用新的documentID将其保存到firestore时,除非刷新页面,否则它将简单地覆盖以前的文档。

To be specific, when I press the button the data was stored in the document called 具体来说,当我按下按钮时,数据存储在名为
-LKV8ZPIAUwk329KqMac . -LKV8ZPIAUwk329KqMac When I selected different data and tried to save to new document, it again was saved in 当我选择其他数据并尝试保存到新文档时,它再次保存在
-LKV8ZPIAUwk329KqMac

Is there a way when I press it saves to a new document each time :) Thank you in advance. 有没有一种方法,当我按下它时,它会每次保存到一个新文档中:)预先谢谢。

Future _addAll(BuildContext context, List chooseFood, List chooseField, String date, String time,
double latitude, double longitude, String text, String uid, String partnerUid) async {
  _controller.clear(); //clear text message
  var sendRequestID; //variable to store documentID
  List connections = new List();

auth.currentUser().then((user) async { //save data to collection('connect')
await docRef.setData({
  'createdTime': DateTime.now(),
  'foods': chooseFood,
  'fields': chooseField,
  'meet_date': date,
  'meet_time': time,
  'latitude': latitude,
  'longitude': longitude,
  'one_word': text,
}).whenComplete(() {
  sendRequestID = docRef.documentID;
  print("DB 저장 $sendRequestID");
  connections.add(sendRequestID);
}).catchError((e) => print(e));

await docRef.collection('connected_users').document('send_request').setData({ //create collection inside a collection
  'email': user.email,
  'name': user.displayName,
  'photoUrl': user.photoUrl
}).catchError((e)=>print(e));
}

You can use collection(...).add(data) , which is the equivalent of collection(...).document().setData(data) . 您可以使用collection(...).add(data) ,它等效于collection(...).document().setData(data)
This will add a new document . 这将添加一个新文档 You will have to use this on a CollectionReference . 您将不得不在CollectionReference上使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM