Firebase 正在创建新文档而不是更新

Firebase 正在创建新文档而不是更新,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我在编辑页面上有一个onPress,它应该在编辑和保存时更新文档 但是,它目前正在使用该数据创建一个新文档 onPressed: () async { //controllers... await updateContact(context); Navigator.pop(context, widget.contact); } 我不知道如何最好地解决这个问题。是的,使用set()将覆盖firestore中已经存在的所有数据 是的,使用update是一种方法,但是请记住不要在整个对象上

我在编辑页面上有一个
onPress
,它应该在编辑和保存时更新文档

但是,它目前正在使用该数据创建一个新文档

onPressed: () async {

//controllers...

await updateContact(context);
Navigator.pop(context, widget.contact);
}

我不知道如何最好地解决这个问题。

是的,使用
set()
将覆盖firestore中已经存在的所有数据

是的,使用
update
是一种方法,但是请记住不要在整个对象上调用
.toJson()
,因为update只接受需要更新的字段。 因此,如果使用整个对象进行更新,它将再次创建一个新对象

你可以这样通过

.update({'name': oneController.text, 'birth': twoContorller.text, 'email': threeController.text});

或者,您也可以使用
set(setOptions:setOptions(merge:true))
这将只更新文档中已更改的字段。

谢谢Sameer。我把
集合(setOptions:setOptions(merge:true))
放在哪里?@ashf而不是
update()
,使用
set()
setOptions()
IDE将自动完成,只需点击ctrl+space
集合(setOptions(merge:true))
正在提供
“参数类型'SetOptions'无法分配给参数类型'Map'
好的,看起来firebase插件版本中可能发生了更改,有关详细信息,请参阅文档,我建议您现在最好使用
更新
。或者稍等,是否正在放置SetOptions()在json之前?确保json是第一个参数,然后是SetOptions
 Future updateContact(context) async {
final uid = await TheProvider.of(context).auth.getCurrentUID();
await db
    .collection('userData')
    .doc(uid)
    .collection('Contacts')
    .doc(widget.contact.documentId)
    .set({
  'Name': oneController.text,
  'PhoneNumber': int.tryParse(twoController.text),
  'Location': threeController.text,
  'Rating': int.tryParse(fourController.text),
  'Instagram': fiveController.text,
  'Birthday': int.tryParse(sixController.text),
  'Notes': sevenController.text},
   SetOptions(merge: true));
Contact.fromSnapshot(DocumentSnapshot snapshot) :
//...
documentId = snapshot.id;
.update({'name': oneController.text, 'birth': twoContorller.text, 'email': threeController.text});