Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Loop Maps


Share

Dart Loop Maps

You can loop through a map with a for-in loop, like you do with a list.

A map has three things you can loop over:

  • keys: all the keys
  • values: all the values
  • entries: all the key and value pairs

The loop always goes through the items in the order they were added.


Loop Through the Keys

Loop over keys to get each key:

Example

void main() {
  var capitals = {
    'Norway': 'Oslo',
    'Sweden': 'Stockholm',
    'Denmark': 'Copenhagen',
  };
  for (var country in capitals.keys) {
    print(country);
  }
}
Try it Yourself »

Result:

Norway
Sweden
Denmark

You can use the key to look up its value inside the loop.

The ! tells Dart that the value is not null, which is safe here because the key comes from the map itself:

Example

void main() {
  var capitals = {
    'Norway': 'Oslo',
    'Sweden': 'Stockholm',
    'Denmark': 'Copenhagen',
  };
  for (var country in capitals.keys) {
    String city = capitals[country]!;
    print('$city is in $country');
  }
}
Try it Yourself »

Result:

Oslo is in Norway
Stockholm is in Sweden
Copenhagen is in Denmark


Loop Through the Values

Loop over values when you only need the values:

Example

void main() {
  var scores = {'Tom': 85, 'Anna': 92, 'Linus': 78};
  var total = 0;
  for (var score in scores.values) {
    total += score;
  }
  print('Total: $total');
}
Try it Yourself »

Result:

Total: 255

Loop Through the Entries

An entry is one key and value pair.

Each entry has a key and a value property:

Example

void main() {
  var scores = {'Tom': 85, 'Anna': 92, 'Linus': 78};
  for (var entry in scores.entries) {
    print('${entry.key} got ${entry.value} points');
  }
}
Try it Yourself »

Result:

Tom got 85 points
Anna got 92 points
Linus got 78 points

This is the best choice when you need both the key and the value.

You can also use if inside the loop:

Example

void main() {
  var stock = {'apples': 12, 'pears': 0, 'kiwis': 5};
  for (var entry in stock.entries) {
    if (entry.value == 0) {
      print('${entry.key} are sold out');
    }
  }
}
Try it Yourself »

Result:

pears are sold out

The forEach() Method

A map also has a forEach() method.

You give it a function with two parameters, the key and the value, and it runs the function once for each pair:

Example

void main() {
  var scores = {'Tom': 85, 'Anna': 92, 'Linus': 78};
  scores.forEach((name, score) {
    print('$name: $score');
  });
}
Try it Yourself »

Result:

Tom: 85
Anna: 92
Linus: 78

Tip: Most Dart code uses a for-in loop over entries instead of forEach().

A for-in loop also lets you use break and continue.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.