Maps in Dart are collections of key-value pairs, where each key is unique. They are similar to dictionaries in other programming languages. Maps are highly useful when you need to associate values with unique keys and retrieve them efficiently.
Key Concepts
- Key-Value Pairs: Each entry in a map is a pair consisting of a key and a value.
- Unique Keys: Keys in a map must be unique. If you try to add a duplicate key, the new value will overwrite the existing one.
- Dynamic and Typed Maps: Maps can be dynamically typed or have specific types for keys and values.
Creating Maps
Using Literals
You can create a map using map literals:
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; print(fruits); // Output: {apple: 2, banana: 5, orange: 3} }
Using the Map Constructor
You can also create a map using the Map
constructor:
void main() { var fruits = Map<String, int>(); fruits['apple'] = 2; fruits['banana'] = 5; fruits['orange'] = 3; print(fruits); // Output: {apple: 2, banana: 5, orange: 3} }
Accessing Map Elements
You can access map elements using their keys:
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; print(fruits['apple']); // Output: 2 }
Modifying Maps
Adding Elements
You can add elements to a map by assigning a value to a new key:
void main() { var fruits = { 'apple': 2, 'banana': 5 }; fruits['orange'] = 3; print(fruits); // Output: {apple: 2, banana: 5, orange: 3} }
Updating Elements
You can update elements by assigning a new value to an existing key:
void main() { var fruits = { 'apple': 2, 'banana': 5 }; fruits['banana'] = 6; print(fruits); // Output: {apple: 2, banana: 6} }
Removing Elements
You can remove elements using the remove
method:
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; fruits.remove('banana'); print(fruits); // Output: {apple: 2, orange: 3} }
Iterating Over Maps
You can iterate over the keys, values, or entries of a map:
Iterating Over Keys
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; for (var key in fruits.keys) { print(key); // Output: apple, banana, orange } }
Iterating Over Values
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; for (var value in fruits.values) { print(value); // Output: 2, 5, 3 } }
Iterating Over Entries
void main() { var fruits = { 'apple': 2, 'banana': 5, 'orange': 3 }; for (var entry in fruits.entries) { print('${entry.key}: ${entry.value}'); // Output: apple: 2, banana: 5, orange: 3 } }
Practical Exercises
Exercise 1: Create and Manipulate a Map
Task: Create a map to store the names and ages of three people. Add a new person to the map, update the age of one person, and remove another person. Print the final map.
Solution:
void main() { var people = { 'Alice': 30, 'Bob': 25, 'Charlie': 35 }; // Add a new person people['David'] = 40; // Update the age of one person people['Alice'] = 31; // Remove another person people.remove('Bob'); print(people); // Output: {Alice: 31, Charlie: 35, David: 40} }
Exercise 2: Iterate Over a Map
Task: Create a map to store the names and scores of three students. Iterate over the map to print each student's name and score.
Solution:
void main() { var students = { 'John': 85, 'Emma': 92, 'Sophia': 78 }; for (var entry in students.entries) { print('${entry.key}: ${entry.value}'); } // Output: // John: 85 // Emma: 92 // Sophia: 78 }
Common Mistakes and Tips
- Using Non-Unique Keys: Remember that keys in a map must be unique. Adding a duplicate key will overwrite the existing value.
- Null Values: Be cautious when accessing keys that might not exist in the map, as it will return
null
.
Conclusion
Maps are a powerful and flexible way to store and manipulate key-value pairs in Dart. Understanding how to create, access, modify, and iterate over maps is essential for effective Dart programming. In the next module, we will explore more advanced collections and their uses.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure