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<int, String> 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. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close