calculateDelta static method
Calculate which fields changed between old and new versions.
Returns only the fields that are different.
Example:
final oldDoc = {'title': 'Old', 'done': false, 'priority': 5};
final newDoc = {'title': 'Old', 'done': true, 'priority': 5};
final delta = DeltaCalculator.calculateDelta(oldDoc, newDoc);
// Returns: {'done': true}
Implementation
static Map<String, dynamic> calculateDelta(
Map<String, dynamic> oldDocument,
Map<String, dynamic> newDocument,
) {
final delta = <String, dynamic>{};
// Check for changed or new fields
for (final key in newDocument.keys) {
final oldValue = oldDocument[key];
final newValue = newDocument[key];
if (!_areEqual(oldValue, newValue)) {
delta[key] = newValue;
}
}
// Check for removed fields (set to null)
for (final key in oldDocument.keys) {
if (!newDocument.containsKey(key)) {
delta[key] = null;
}
}
return delta;
}