set method
Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.
data a map of the fields and values for the document (or a T that the converter turns into one).
options an object to configure the set behavior. By default (SetOptions()) the document is replaced
with data; SetOptions(merge: true) only writes the fields in data and keeps the others, and
SetOptions.mergeFields only writes the listed fields.
Returns a Future that will be resolved when the write finishes, that is when the backend has acknowledged it.
The write is applied to the local cache at once, so reads and listeners see it immediately; while the client is
offline the future does not complete. The future fails with an ArgumentError when data holds a value
Firestore cannot store, and with a FirestoreException when the backend rejects the write (for example
FirestoreErrorCode.permissionDenied).
Values can be null, bool, int, double, String, Timestamp, DateTime, GeoPoint, Blob,
DocumentReference, VectorValue, lists and maps of those, and the sentinels of FieldValue such as
FieldValue.serverTimestamp().
final DocumentReference<Map<String, Object?>> sf = firestore.doc(
'cities/SF',
);
await sf.set({
'name': 'San Francisco',
'population': 870000,
'updated': FieldValue.serverTimestamp(),
});
// Change one field and keep the others.
await sf.set({'capital': false}, const SetOptions(merge: true));
Implementation
Future<void> set(T data, [SetOptions? options]) =>
_set(data, options, 'DocumentReference.set');