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 Maps


Share

Dart Maps

A map stores data in key and value pairs.

The key is the name you use to find a value, and the value is the data you store.

You write a map with curly brackets {}, and put a colon : between each key and its value:

Example

void main() {
  var car = {
    'brand': 'Ford',
    'model': 'Mustang',
    'color': 'red',
  };
  print(car);
}
Try it Yourself »

Result:

{brand: Ford, model: Mustang, color: red}

A comma after the last pair is allowed.

It makes it easy to add more pairs later.


Keys Are Unique

Each key can only appear once in a map.

Values can be the same, but keys cannot.

If you use the same key twice, only the last value is kept:

Example

void main() {
  var ages = {
    'Tom': 30,
    'Anna': 25,
    'Tom': 31,
  };
  print(ages);
}
Try it Yourself »

Result:

{Tom: 31, Anna: 25}

Note: The program still runs, but code editors show a warning for the repeated key, because it is almost always a mistake.



Typed Maps

A map has two types: the type of the keys and the type of the values.

You write them in angle brackets, like Map<String, int>.

Here, every key is a String and every value is an int:

Example

void main() {
  Map<String, int> scores = {
    'Tom': 85,
    'Anna': 92,
    'Linus': 78,
  };
  print(scores);
}
Try it Yourself »

Result:

{Tom: 85, Anna: 92, Linus: 78}

The keys do not have to be strings.

This map uses int keys and String values:

Example

void main() {
  Map<int, String> days = {
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
  };
  print(days[2]);
}
Try it Yourself »

Result:

Tuesday

The code days[2] gets the value of the key 2.

You will learn more in the Access Map Items chapter.

If you add a value of the wrong type, the program does not run:

Example

void main() {
  Map<String, int> scores = {
    'Tom': 85,
    'Anna': 'high',
  };
  print(scores);
}
Try it Yourself »

Result:

main.dart:4:13: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
    'Anna': 'high',
            ^

Empty Maps

To create an empty map, write the types in angle brackets before {}, or write the type on the variable:

Example

void main() {
  var prices = <String, double>{};
  Map<String, double> stock = {};
  print(prices);
  print(stock.isEmpty);
}
Try it Yourself »

Result:

{}
true

The isEmpty property is true when the map has no pairs.


Keys, Values and Length

Use keys to get all the keys, and values to get all the values.

Use length to get the number of pairs:

Example

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

Result:

(Norway, Sweden, Denmark)
(Oslo, Stockholm, Copenhagen)
3

The keys and values are printed in parentheses, because they are not lists.

Use toList() if you need them as a list:

Example

void main() {
  var capitals = {
    'Norway': 'Oslo',
    'Sweden': 'Stockholm',
  };
  List<String> countries = capitals.keys.toList();
  print(countries);
}
Try it Yourself »

Result:

[Norway, Sweden]

A map keeps the pairs in the order you added them.

Read more in Access Map Items and Loop Maps.



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.