valueOf method
V
valueOf(
- K key, {
- required V initialValue,
Returns the value of the given key
, if it exists, and adds it if it doesn't
If the the key
does not exist, the given initialValue
is added and returned
final animalRatings = { "aardvark": 1, "baboon": 2 };
final aardvarkRating = animalRatings.valueOf("aardvark", initialValue: 0); // Returns existing value of 1
final baboonRating = animalRatings.valueOf("baboon", initialValue: 2); // Adds baboon and returns the value of 2
Implementation
V valueOf(K key, {required V initialValue}) {
if (containsKey(key)) return this[key]!;
this[key] = initialValue;
return initialValue;
}