update method
Updates fields in the document referred to by this DocumentReference. If no document exists yet, the update will fail.
data a map of field/value pairs to update. Each key is a String, in which dots separate nested fields, or a
FieldPath, for a field name that contains a dot. The fields that are not in data keep their values. Use
FieldValue.delete() as a value to remove a field.
Returns a Future that will be resolved when the write finishes, that is when the backend has acknowledged it. The update is applied to the local cache at once; while the client is offline the future does not complete. When the document does not exist the backend rejects the update and the future fails with a FirestoreException with the code FirestoreErrorCode.notFound. The future fails with an ArgumentError for an invalid field path or value.
await firestore.doc('cities/SF').update({
'population': FieldValue.increment(1000),
'stats.visits': 12, // a nested field
'nickname': FieldValue.delete(),
FieldPath(['stats', 'last.year']): 870000, // a field name with a dot
});
Implementation
Future<void> update(Map<Object, Object?> data) async {
final UserDataParsedUpdateData parsedData = firestore.dataConverter
.parseUpdateMap(
data,
methodName: 'DocumentReference.update',
targetDoc: key,
);
await voidErrorTransformer(
() => firestore.client.write(<Mutation>[
parsedData.toMutation(key, Precondition(exists: true)),
]),
);
}