WriteBatch class
A set of writes that Firestore applies together, as a single atomic unit: either all of them or none.
Get a batch from FirebaseFirestore.batch, add writes with set, update, update and delete, then call commit. None of the writes is applied, not even to the local cache, until commit is called. FirebaseFirestore.runBatch does the same and commits for you.
final DocumentReference<Map<String, Object?>> sf = firestore.doc('cities/SF');
final DocumentReference<Map<String, Object?>> la = firestore.doc('cities/LA');
await firestore
.batch()
.set(sf, <String, Object?>{'name': 'San Francisco'})
.update(la, <String, Object?>{'population': FieldValue.increment(1)})
.delete(firestore.doc('cities/OLD'))
.commit();
A batch does not read, so it cannot make a write depend on the current value of a document; use FirebaseFirestore.runTransaction for that. In return a batch works offline: commit applies the writes to the local cache at once and sends them when the client is online again, while a transaction needs the backend.
The client sets no limit on the number of writes. The backend rejects a commit request larger than 10 MiB, and allows at most 500 field transforms (such as FieldValue.serverTimestamp or FieldValue.increment) per document. Unlike in earlier versions of the Firebase SDKs, there is no limit of 500 writes per batch.
A batch belongs to the FirebaseFirestore instance that created it: every DocumentReference passed to it must come from the same instance.
Subclassing Note: Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
commit(
) → Future< void> - Commits all the writes of this batch as a single atomic unit.
-
delete(
DocumentReference< Object?> documentRef) → WriteBatch -
Adds a delete of the document
documentRef. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set<
T extends Object?> (DocumentReference< T> documentRef, T data, [SetOptions? options]) → WriteBatch -
Adds a write of
datato the documentdocumentRef: the document is created if it does not exist, and replaced if it does. -
toString(
) → String -
A string representation of this object.
inherited
-
update(
DocumentReference< Object?> documentRef, Map<Object, Object?> data) → WriteBatch -
Adds an update of the fields in
dataof the existing documentdocumentRef. Fields that are not indatakeep their values.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited