Created
August 24, 2022 15:31
-
-
Save stijn1989/896d0e949c65e933691421df899eff93 to your computer and use it in GitHub Desktop.
Map helpers with lists as values
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
///This extension contains some usefull helpers for maps | |
///which value contains a list of [V] objects. | |
extension MapList<K, V> on Map<K, List<V>> | |
{ | |
///Add [value] to the list of [key]. | |
///If this map doesn't contain the [key], | |
///a new list is added with [value] as only value. | |
void add(K key, V value) | |
{ | |
if(containsKey(key) && this[key] is List<V>) { | |
this[key]!.add(value); | |
} else { | |
this[key] = [value]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment