batch method

WriteBatch batch()

Returns a new WriteBatch, which applies several writes as a single atomic operation.

Add writes with WriteBatch.set, WriteBatch.update and WriteBatch.delete, then call WriteBatch.commit: either all the writes are applied or none. Unlike a transaction a batch reads nothing, so it also works offline: the writes are applied to the local cache at the commit and sent when the client is online, and the future of WriteBatch.commit completes when the backend has acknowledged them.

There is no limit on the number of writes in a batch, but its commit request must not exceed the maximum API request size of 10 MiB, and a single document takes at most 500 field transforms (such as FieldValue.serverTimestamp or FieldValue.increment).

final WriteBatch batch = firestore.batch();
batch.set(firestore.doc('cities/NYC'), {'name': 'New York City'});
batch.update(firestore.doc('cities/SF'), {'population': 1000000});
batch.delete(firestore.doc('cities/LA'));
await batch.commit();

Implementation

WriteBatch batch() {
  return WriteBatchInternal.create(this);
}