Skip to main content

Iterate Through a Dart Map

In Dart, a map is a collection of key-value pairs. In this article, I will describe several different ways to iterate through this kind of structure, each technique using its own merits.

Map map = {
  0: "LG",
  12: "Samsung",
  17: "Apple",
  20: "Lenovo",
  24: "Asus",
  29: "HP",
};

map.forEach((key, value) {
  print("$key: $value");
});
for(var key in map.keys){
  print("$key: $map[$key]");
}
for (MapEntry e in map.entries) {
  print("${e.key}: ${e.value}");
}
for (var value in map.values) {
  print(value);
}

By continuing to use the site, you agree to the use of cookies.