setMerge<T> function

T? setMerge<T>(
  1. T? existingData,
  2. T? newData, [
  3. SetOptions? options
])

Attempt to replicate how the Cloud Firestore server sets document data in order to support client-side cache changes. TODO: The server merge logic is more advanced than this, figure out how replicate it fully on the client for cache-first updates.

Implementation

T? setMerge<T>(T? existingData, T? newData, [SetOptions? options]) {
  final shouldMerge = options?.merge ?? false;

  if (newData == null) {
    return null;
  }

  if (newData is Map<String, dynamic> &&
      existingData is Map<String, dynamic> &&
      shouldMerge) {
    return {
      ...existingData,
      ...newData,
    } as T;
  }

  return newData;
}