set<T> method

Future<WriteResult> set<T>(
  1. DocumentReference<T> ref,
  2. T data, {
  3. SetOptions? options,
})

Write to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created.

  • ref: A reference to the document to be set.
  • data: The object to serialize as the document.

Returns a Future that resolves with the result of the write. If the write fails, the Future is rejected with a BulkWriterError.

Example:

final bulkWriter = firestore.bulkWriter();
final documentRef = firestore.collection('col').doc();

bulkWriter
  .set(documentRef, {'foo': 'bar'})
  .then((result) {
    print('Successfully executed write at: $result');
  })
  .catchError((err) {
    print('Write failed with: $err');
  });

Implementation

Future<WriteResult> set<T>(
  DocumentReference<T> ref,
  T data, {
  SetOptions? options,
}) {
  _verifyNotClosed();
  return _enqueue(
    ref,
    'set',
    (batch) => batch.set(ref, data, options: options),
  );
}