Dart: Should I prefer to iterate over Map.entries or Map.values?
We should iterate map based on the requirement.
If we required to iterate only keys
map.keys.forEach((k) => print("Key : $k"));
If we required to iterate only values
map.values.forEach((v) => print("Value: $v"));
If we required to iterate both key values.
map.forEach((k, v) => print("Key : $k, Value : $v"));
If you iterate over entries a MapEntry
instance is created for every key-value combination.
If you only need values, iterating map.values
is more efficient.