set<T extends Object?> method
- DocumentReference<
T> documentRef, - T data, [
- SetOptions? options
Writes data to the document documentRef when the transaction
commits: the document is created if it does not exist, and replaced if
it does.
With options set to SetOptions(merge: true),
SetOptions.mergeFields, data is
merged into the existing document instead. When documentRef has a
converter, data is a T and is converted with
FirestoreDataConverter.toFirestore.
If the transaction read documentRef before, the commit checks that the
document has not changed since.
transaction.set(
firestore.doc('cities/SF'),
<String, Object?>{'visited': true},
const SetOptions(merge: true),
);
Throws an ArgumentError if data holds a value Firestore cannot store,
or if documentRef belongs to another FirebaseFirestore instance. Throws a
FirestoreException with FirestoreErrorCode.invalidArgument in a
read-only transaction.
Returns this transaction, so that calls can be chained.
Implementation
Transaction set<T extends Object?>(
DocumentReference<T> documentRef,
T data, [
SetOptions? options,
]) {
final SetOptions? userOptions = options;
options ??= const SetOptions();
_firestore.validateReference(documentRef);
checkNotNull<Object>(data, 'Provided data must not be null.');
final Map<String, Object?> rawData = toRawData<T>(
data,
documentRef.converter,
userOptions,
);
final UserDataParsedSetData parsed = options.merge
? _firestore.dataConverter.parseMergeData(
rawData,
options.fieldMask,
methodName: 'Transaction.set',
targetDoc: documentRef.key,
hasConverter: documentRef.converter != null,
)
: _firestore.dataConverter.parseSetData(
rawData,
methodName: 'Transaction.set',
targetDoc: documentRef.key,
hasConverter: documentRef.converter != null,
);
_transaction.set(documentRef.key, parsed);
return this;
}