Created
April 5, 2022 05:21
-
-
Save mirkancal/ae9357d375446cd0f22f3f88c739c610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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