Lesson 3

Navigating Through Maps in Dart: Understanding Key-Value Pairs

Introduction to Map Data Structures in Dart

Welcome aboard! Today, we take a trip through Dart's Map data structures. In the real world, a map helps us track a location. Similarly, a Map in Dart connects a unique key to a related value. As we journey through various countries and their capitals, Maps in Dart assist us by connecting each country (key) to its capital (value).

Understanding Map Data Structures

In Dart, a Map organizes key-value pairs, akin to a dictionary that connects a word (key) with its meaning (value). For instance, in our case, a country serves as the key, and its capital is the value.

Below is a small Map, referred to as countryCapital:

1Map<String, String> countryCapital = { 2 "USA": "Washington D.C.", 3 "India": "New Delhi", 4 "France": "Paris", 5};
Declaration of Maps in Dart

Declaring an empty Map in Dart is straightforward. We use the {} syntax alongside the key and value data types.

1Map<String, String> countryCapital = {};

To declare a Map with initial values, we specify the key-value pairs:

1Map<String, String> countryCapital = { 2 "USA": "Washington D.C.", 3 "India": "New Delhi", 4 "France": "Paris", 5};
Accessing Values from a Map

After declaring a Map, you may want to retrieve a specific value using its key. For example, to get the capital of France, you simply use:

1print("The capital of France is ${countryCapital['France']}"); // Prints "The capital of France is Paris"

By providing the key ('France' in this case) inside square brackets [ ] following the Map name (countryCapital), Dart fetches the corresponding value.

Operations on Maps

Now, let's manipulate our Maps. Dart allows us to efficiently add, update, retrieve values from a Map.

Adding a Key/Value Pair to Map: To add a new country and its capital to our Map:

1countryCapital["Japan"] = "Tokyo"; // Japan and its capital, Tokyo, have been added

Updating a Map Value: To change the capital of a country (such as updating the capital of India to 'Mumbai'):

1countryCapital["India"] = "Mumbai"; // India's capital has been updated to Mumbai

Retrieving All Keys and Values: To retrieve all countries (keys) and capitals (values), we use the keys and values properties:

1print(countryCapital.keys); // This will print all the keys: Countries 2print(countryCapital.values); // This will print all the values: Capitals
Understanding Map Methods

Let's familiarize ourselves with Dart's built-in Map methods.

  • .length: Count total key-value pairs in our Map.
  • .isEmpty and .isNotEmpty: Determine whether our Map is devoid of entries or not.
  • .remove(key): Eliminate a key-value pair from the Map.
  • .containsKey(key): Check if a certain key exists within the Map.
1print(countryCapital.length); // Counts the total of key-value pairs 2print(countryCapital.isEmpty); // Checks if the map does not have any key-value pairs 3countryCapital.remove("France"); // Removes a key-value pair 4bool keyExists = countryCapital.containsKey("USA"); // Checks for the existence of a key 5print(keyExists); // Prints 'true' if the key exists, otherwise 'false'
Lesson Summary and Practice

Great job! The key points from today's lesson include Dart's Map data structures, and how to declare, initialize, and perform operations on them using real-life examples. Prepare for apt practice exercises to apply these concepts. Remember – practice is crucial to mastering these concepts. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.