I'm trying to make changes to a specific field in a DocumentSnapshot, but I cant figure out how to get the update method to work. I can't even find any documentation to help me figure it out.
whatever.data().update("Availability", (newValue) => whatever);
whatever is a documentsnapshot,'Availabilty' is the field I want to update, and 'newValue' is the value I want to update the field in the fetched object itself and not in Firestore.
The DocumentSnapshot
has a reference
property which is the DocumentReference
of this snapshot.
Then you can update the document like this:
whatever.reference.update({Availability: "NewValue"})
Edit: OP wanted to updated local variable only and not the data in Firestore which can be done by assigning the data to a variable and update it.
var myData = whatever.data()
myData['property'] = 'value'
Try this
whatever.doc().update({
"Availability": newValue,
});
FirebaseFirestore.instance
.collection('YOURCOLLECTION')
.doc(DOCUMENTID)
.update({'Availability': newValue}).then((value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Success'),
),
);
}).onError((error, stackTrace) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error while updating document'),
),
);
});
YOURCOLLECTION
is the name of your collection and DOCUMENTID
is your document Id for which you want to update the data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.