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).
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
:
Dart1Map<String, String> countryCapital = { 2 "USA": "Washington D.C.", 3 "India": "New Delhi", 4 "France": "Paris", 5};
Declaring an empty Map in Dart is straightforward. We use the {}
syntax alongside the key and value data types.
Dart1Map<String, String> countryCapital = {};
To declare a Map with initial values, we specify the key-value pairs:
Dart1Map<String, String> countryCapital = { 2 "USA": "Washington D.C.", 3 "India": "New Delhi", 4 "France": "Paris", 5};
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:
Dart1print("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.
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:
Dart1countryCapital["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'):
Dart1countryCapital["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:
Dart1print(countryCapital.keys); // This will print all the keys: Countries 2print(countryCapital.values); // This will print all the values: Capitals
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.Dart1print(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'
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!