Dart Lesson 13 – Maps | Dataplexa

Maps in Dart

In this lesson, you will learn about Maps in Dart. Maps are used to store data in key–value pairs, making them one of the most powerful and widely used data structures in real-world applications.

Whenever you see data represented as labels with values—such as user profiles, product details, configuration settings, or API responses—you are essentially working with maps.


What Is a Map?

A map stores data as keys mapped to values. Each key in a map must be unique, while values can be duplicated.

Think of a map like a dictionary:

  • Key → Word
  • Value → Meaning

Creating a Map

Let’s create a simple map that stores employee IDs and their names.

Map employees = {
  101: "Alice",
  102: "Bob",
  103: "Charlie"
};

print(employees);

Here:

  • int is the key type
  • String is the value type

Accessing Values Using Keys

You can access values in a map by providing the key.

print(employees[102]);
print(employees[105]);

If a key does not exist, Dart returns null.


Real Example: Student Marks System

Let’s store student names and their exam scores.

Map studentMarks = {
  "Rahul": 88,
  "Anita": 92,
  "Suresh": 76
};

print(studentMarks["Anita"]);

Maps are ideal for such structured data.


Adding New Entries to a Map

You can add new key–value pairs dynamically.

studentMarks["Priya"] = 85;
print(studentMarks);

If the key already exists, its value is updated.


Updating Existing Values

Let’s update a student’s score.

studentMarks["Rahul"] = 90;
print(studentMarks);

Removing Entries from a Map

Use the remove() method to delete an entry.

studentMarks.remove("Suresh");
print(studentMarks);

Checking If a Key Exists

Before accessing a value, you may want to check if a key exists.

print(studentMarks.containsKey("Anita"));
print(studentMarks.containsKey("Kiran"));

Iterating Through a Map

You can loop through a map using forEach.

studentMarks.forEach((name, score) {
  print("$name scored $score");
});

This is extremely useful for reports and dashboards.


Getting Keys and Values Separately

You can retrieve all keys or values from a map.

print(studentMarks.keys);
print(studentMarks.values);

Map Length

The length property returns the number of key–value pairs.

print("Total students: ${studentMarks.length}");

Map vs List vs Set

  • List → Indexed collection, allows duplicates
  • Set → Unique values only
  • Map → Key–value pairs

Real-World Use Cases

  • User profiles (username → details)
  • Configuration settings
  • API JSON responses
  • Database-like in-memory storage

📝 Practice Exercises


Exercise 1

Create a map that stores country names and their capitals.

Exercise 2

Add a new country to the map and update an existing one.

Exercise 3

Loop through the map and print each country with its capital.


✅ Practice Answers


Answer 1

Map capitals = {
  "India": "New Delhi",
  "USA": "Washington D.C.",
  "Japan": "Tokyo"
};

print(capitals);

Answer 2

capitals["France"] = "Paris";
capitals["USA"] = "Washington";
print(capitals);

Answer 3

capitals.forEach((country, capital) {
  print("$country → $capital");
});

What’s Next?

In the next lesson, you will learn about Collection Methods in Dart.

These methods allow you to filter, transform, and process collections efficiently.