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 Access Map Items


Share

Dart Access Map Items

You get a value from a map by writing its key inside square brackets []:

Example

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

Result:

Ford
Mustang

Missing Keys Return null

If the key is not in the map, you get null:

Example

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

Result:

null

Because of this, the type of car['brand'] is String?, not String.

The question mark means the value can be null, as you learned in Null Safety.

So you cannot put it straight into a String variable:

Example

void main() {
  var car = {'brand': 'Ford'};
  String brand = car['brand'];
  print(brand);
}
Try it Yourself »

Result:

main.dart:3:21: Error: A value of type 'String?' can't be assigned to a variable of type 'String'.
  String brand = car['brand'];
                    ^

A simple fix is to give a default value with the ?? operator:

Example

void main() {
  var car = {'brand': 'Ford'};
  String brand = car['brand'] ?? 'unknown';
  String color = car['color'] ?? 'unknown';
  print(brand);
  print(color);
}
Try it Yourself »

Result:

Ford
unknown


Check if a Key Exists

Use containsKey() to check if a key is in the map.

Use containsValue() to check if a value is in the map:

Example

void main() {
  var stock = {'apples': 12, 'pears': 0};
  print(stock.containsKey('apples'));
  print(stock.containsKey('kiwis'));
  print(stock.containsValue(0));
}
Try it Yourself »

Result:

true
false
true

Add and Change Items

To add a new item, write a new key in square brackets and give it a value.

A new key is added at the end of the map.

If the key already exists, the old value is replaced:

Example

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

Result:

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

putIfAbsent() adds an item only if the key is not in the map yet.

The value is given by a small function, written as () => value:

Example

void main() {
  var stock = {'apples': 12};
  stock.putIfAbsent('apples', () => 50);
  stock.putIfAbsent('pears', () => 7);
  print(stock);
}
Try it Yourself »

Result:

{apples: 12, pears: 7}

update() changes the value of a key, based on its old value:

Example

void main() {
  var stock = {'apples': 12, 'pears': 7};
  stock.update('apples', (value) => value + 3);
  print(stock);
}
Try it Yourself »

Result:

{apples: 15, pears: 7}

If the key does not exist, update() throws an error.

Add ifAbsent to give a value to use when the key is missing:

Example

void main() {
  var stock = {'apples': 12};
  stock.update('kiwis', (value) => value + 1, ifAbsent: () => 1);
  print(stock);
}
Try it Yourself »

Result:

{apples: 12, kiwis: 1}

Note: The arrow => creates a short function.

In () => 50, the function gets no value, and in (value) => value + 3, it gets the old value in value.

You will learn more about it in Arrow Functions.


Remove Items

Use remove() to remove a key and its value.

Use clear() to remove all items:

Example

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

Result:

{brand: Ford, model: Mustang}
{}


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.