Reputation: 217
I have an onPress
on an edit page - it should update the document when edited and saved.
However, it is currently creating a new document with that data instead.
onPressed: () async {
//controllers...
await updateContact(context);
Navigator.pop(context, widget.contact);
}
.
final db = FirebaseFirestore.instance;
.
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;
I am not sure how to best resolve this.
Upvotes: 0
Views: 64
Reputation: 1166
Yes, using set()
will override all the data already present in firestore.
and yes, using update
is the way to go, but keep in mind not to call .toJson()
on the entire object as update only takes the fields that are needed to be updated.
So if you update with the entire object, it'll create a new one again.
You could pass it like this
.update({'name': oneController.text, 'birth': twoContorller.text, 'email': threeController.text});
alternatively, you can also use set( setOptions: SetOptions(merge:true))
this will update only the fields that have changed in the document.
Upvotes: 1