mergeTwoMaps function
- merges to maps into single one
- if the key found in both maps it will use the one from the second map
Implementation
Map<String, Object?>? mergeTwoMaps(
Map<String, Object?>? a,
Map<String, Object?>? b,
) {
/// if a is null then return b
/// if b is null then return a
/// if both is null then return null 🤷♀️
if (a == null || b == null) return b ?? a;
/// the result of the merge
final result = Map.of(a);
/// ad the key from two to one
for (final key in b.keys) {
/// if the value is Map then merge them
if (b[key] is Map || a[key] is Map) {
final aValueIsMap = a[key] is Map;
final bValueIsMap = b[key] is Map;
result[key] = mergeTwoMaps(
aValueIsMap ? a[key] as Map<String, Object?>? : {},
bValueIsMap ? b[key] as Map<String, Object?>? : {},
);
} else {
/// else just use the value from the second map
result[key] = b[key];
}
}
return result;
}