Menu
Coddy logo textTech

Creating a Map

Part of the Fundamentals section of Coddy's Dart journey — lesson 57 of 94.

To create a map in Dart, use curly braces {} with key-value pairs separated by colons. Maps store data as key-value associations.

Create a map with string keys and integer values:

Map<String, int> studentScores = {
  'John': 95,
  'Sarah': 88,
  'Michael': 72
};
print(studentScores);

After executing the above code, the output will be:

{John: 95, Sarah: 88, Michael: 72}

Create an empty map and specify its types:

Map<String, bool> userStatus = {};
print(userStatus);

After executing the above code, the output will be:

{}
challenge icon

Challenge

Beginner

In this challenge, you'll create a simple map that stores student names and their scores. Maps in Dart store data as key-value pairs, where each value is associated with a unique key.

Complete the code below to create a map called studentScores with the following student names and scores:

  • Alice: 95
  • Bob: 85
  • Charlie: 90

After creating the map, the program will print it for you.

Expected output:

{Alice: 95, Bob: 85, Charlie: 90}

Cheat sheet

To create a map in Dart, use curly braces {} with key-value pairs separated by colons:

Map<String, int> studentScores = {
  'John': 95,
  'Sarah': 88,
  'Michael': 72
};

Create an empty map and specify its types:

Map<String, bool> userStatus = {};

Try it yourself

void main() {
  // TODO: Create a map called 'studentScores' that stores student names as keys
  // and their scores as values using the data provided in the task description
  
  // Don't modify the line below - it will print your map
  print(studentScores);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals