diff static method
Returns a Firestore update()-ready map of dot-notation field paths
representing the difference between before and after.
Behavior:
- When
beforeisnull, returns a shallow copy ofafter(top-level keys emitted as-is, no flattening). - When
beforeis deeply equal toafter, returns an empty map so callers can skip the write entirely. - For each key in
after:- If
beforedoes not contain the key, emits{key: afterValue}at the current dot-notation path (no recursion when one side is absent). - If both
beforeValueandafterValueareMap<String, dynamic>, recurses with prefix${currentPath}.${key}. - Else if
DeepCollectionEquality().equals(beforeValue, afterValue), skips the entry. - Else emits
{path: afterValue}.
- If
- When
includeDeletesistrue, also walks keys present inbeforebut missing fromafterand emits{path: FieldValue.delete()}. - Lists are not recursed into; they are compared atomically by
DeepCollectionEqualityand replaced wholesale if not equal.
Implementation
static Map<String, Object?> diff({
required Map<String, dynamic>? before,
required Map<String, dynamic> after,
bool includeDeletes = false,
}) {
if (before == null) {
return Map<String, Object?>.from(after);
}
const equality = DeepCollectionEquality();
if (equality.equals(before, after)) {
return <String, Object?>{};
}
final result = <String, Object?>{};
_collect(
before: before,
after: after,
prefix: '',
includeDeletes: includeDeletes,
result: result,
equality: equality,
);
return result;
}