Skip to content

Instantly share code, notes, and snippets.

@stijn1989
Created August 24, 2022 15:31
Show Gist options
  • Save stijn1989/896d0e949c65e933691421df899eff93 to your computer and use it in GitHub Desktop.
Save stijn1989/896d0e949c65e933691421df899eff93 to your computer and use it in GitHub Desktop.
Map helpers with lists as values
///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