update method

Method to update data in the document.

Parameters:

  • data: The data to be updated in the document.

Example:

documentRef.update({
    'name': Mr. X,
    'age': InAppFieldValue.increment(2),
    'balance': InAppFieldValue.increment(-10.2),
    'hobbies': InAppFieldValue.arrayUnion(['swimming']),
    'skills': InAppFieldValue.arrayRemove(['coding', 'gaming']),
    'timestamp': InAppFieldValue.serverTimestamp(),
    'extra': InAppFieldValue.delete(),
  });

Implementation

Future<InAppDocumentSnapshot?> update(InAppDocument data) {
  return get().then((base) {
    final current = _InAppMerger(base.data).merge(data);
    current[_idField] = id;
    return _db
        ._w(
          reference: reference,
          collectionPath: _p.path,
          collectionId: _p.id,
          documentId: id,
          type: InAppWriteType.document,
          value: current,
        )
        .then(_n)
        .then((_) => _ ? InAppDocumentSnapshot(_id, current) : null);
  });
}