How to Create a Map in Dart

A Map in Dart is a collection of key-value pairs, where each key is unique. Maps are useful for associating values with keys, allowing for efficient data retrieval. Dart provides several ways to create maps, and they can be mutable (modifiable) or immutable (fixed).

1. Creating a Mutable Map

A mutable map can be modified after it is created. You can add, remove, or change key-value pairs in a mutable map.

Example of Creating a Mutable Map

void main() {
// Creating a mutable map of string keys and integer values
Map<string, int> scores = {
'Alice': 90,
'Bob': 85,
'Charlie': 92,
};

// Adding a new key-value pair
scores['David'] = 88;

// Removing a key-value pair
scores.remove('Bob');

// Accessing a value by key
print(scores['Alice']); // Output: 90

// Iterating through the map
scores.forEach((key, value) {
print('$key: $value'); // Output: Alice: 90, Charlie: 92, David: 88
});
}
</string,>

In this example, we create a mutable map that associates names with scores. We add a new key-value pair, remove an existing one, access a value by its key, and iterate through the map to print its contents.

2. Creating an Immutable Map

An immutable map cannot be modified after it is created. You cannot add or remove key-value pairs, but you can access the values.

Example of Creating an Immutable Map

void main() {
// Creating an immutable map using the Map.of constructor
final Map<string, int> immutableScores = Map.unmodifiable({
'Alice': 90,
'Bob': 85,
'Charlie': 92,
});

// Accessing a value by key
print(immutableScores['Bob']); // Output: 85

// Attempting to add a new key-value pair will result in an error
// immutableScores['David'] = 88; // Uncommenting this line will cause an error
}
</string,>

In this example, we create an immutable map using the Map.unmodifiable constructor. We access a value by its key, but attempting to modify the map will result in an error.

3. Creating a Map Using Constructors

You can also create a map using the Map constructor. This is useful when you want to create an empty map or a map with specific key-value pairs.

Example of Creating a Map Using Constructors

void main() {
// Creating an empty map
Map<string, int> emptyMap = Map<string, int>();

// Adding key-value pairs
emptyMap['Alice'] = 90;
emptyMap['Bob'] = 85;

// Accessing values
print(emptyMap['Alice']); // Output: 90
}
</string,></string,>

In this example, we create an empty map and add key-value pairs to it. We then access a value by its key.

4. Summary of Map Creation

  • Mutable Map: Can be modified after creation. Use curly braces or the Map constructor.
  • Immutable Map: Cannot be modified after creation. Use the Map.unmodifiable constructor.
  • Accessing Values: Use the key to retrieve the associated value.

5. Conclusion

Creating maps in Dart is straightforward and flexible. You can create mutable or immutable maps, use constructors, and manipulate the contents as needed. Understanding how to create and work with maps is essential for managing key-value pairs effectively in Dart applications.