update<T> method

Future<NimbostratusDocumentSnapshot<T?>> update<T>(
  1. DocumentReference<T> ref,
  2. T data, {
  3. WritePolicy writePolicy = WritePolicy.serverFirst,
  4. ToFirestore<T>? toFirestore,
  5. NimbostratusFromFirestore<T>? fromFirestore,
})

Implementation

Future<NimbostratusDocumentSnapshot<T?>> update<T>(
  DocumentReference<T> ref,
  T data, {
  WritePolicy writePolicy = WritePolicy.serverFirst,
  ToFirestore<T>? toFirestore,
  NimbostratusFromFirestore<T>? fromFirestore,
}) async {
  final snap = await _update<T>(
    ref,
    data,
    writePolicy: writePolicy,
    toFirestore: toFirestore,
    batch: _batch,
    fromFirestore: fromFirestore,

    /// A [WritePolicy.serverFirst] is not optimistic as it waits for the server response. Otherwise
    /// the batch update will be optimistic since both [WritePolicy.cacheOnly] and [WritePolicy.cacheAndServer]
    /// will optimistically write to the cache first.
    isOptimistic: writePolicy != WritePolicy.serverFirst,
  );

  // The returned snap may not be optimistic if for example, the optimistic update we're trying to perform is already the current value of the
  // document, in which case it won't re-emit any new value.
  if (snap.isOptimistic) {
    _batchOpimisticSnapshots.add(snap);
  }
  return snap;
}