Skip to content

Instantly share code, notes, and snippets.

@mirkancal
Created April 5, 2022 05:21
Show Gist options
  • Save mirkancal/ae9357d375446cd0f22f3f88c739c610 to your computer and use it in GitHub Desktop.
Save mirkancal/ae9357d375446cd0f22f3f88c739c610 to your computer and use it in GitHub Desktop.
void main() {
final firstMap = {"1":[1,2,3], "2": [0,0]};
final secondMap = {"2":[1,1]};
final thirdMap = {
...firstMap,
...secondMap,
};
print(firstMap.merge(secondMap).values);
print(firstMap.values);
}
extension Merger<T, V> on Map<T, List<V>> {
Map<T, List<V>> merge(Map<T, List<V>> second) {
var copy = {...this};
second.forEach((key, value) {
if (copy[key] != null) {
copy[key]!.addAll(value);
} else {
copy[key] = [...value];
}
});
return copy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment