Table of Contents
Concept of Map in Dart
The concept of Map in Dart remains as we discussed three types of collections i.e. List, Set in the last class. The first two types were discussed. Now we are going to learn Map which is the most critical type in collections. In Flutter we mostly use Map. It is to remember that in List data is in an ordered form whereas in Sets, it is stored in an unordered form. Now let’s move to Map.
Map
A map is also used to store multiple values as we have seen in other types. Map stores data in the form of key-value pairs. We use curly braces {} in the map. Its syntax is a bit different from the other two types as shown under:-
void main () {
Map map1 = {“key” : “value”, “key2” : “value2”, “key3”: “value3”};
}
How to write key
In Map, the key is on the left side, and the value is on the right side. We enter Key then use a colon and after that, our value comes. It is recommended that the key should be in snake_case and unique. We cannot assign duplicate keys in it like Set. The values can be of any type like int, String, double, bool, etc. for example:-
void main () {
Map map1 = {1 : “value”, 2 : “value2”, 3 : “value3”};
Map map2 = {true : “value”, false : “value2”};
}
Hybrid Maps
We can also make hybrid-type maps i.e:
Map map3 = {1: “value”, 2.5 : “value 2”, false : “value5”};
In most cases, we use String-type keys. A map can have different types of values and it can be of any type i.e. int, double, String but may not be duplicated. We can also set null values against any key as well. We don’t follow convention for values and it can contain any type/ case of text.
Var Keyword instead of Map
We can also replace Map with the var keyword to create a map like:
var map3 = {1: “value”, 2.5 : “value 2”, false : “value5”};
Dart recommendations in lieu of concept of Map in Dart
As per Dart’s recommended approach, we can also define variable types with Map. But for this purpose, we need to define the key as well with value after Map and follow the pattern like:
void main () {
Map<String, String> map1 = {“key” : “value”, “key2” : “value2”, “key3”: “value3”};
}
A value can also be a List, Set, or map like
void main () {
Map<String, List> map1 = {“key” : [1,2,3], “key2” : [“value2”, “value 3”], “key3”: [true, false]};
}
Concept of Map in Dart with another Map
For a map, we can further add map type as well as we learned before:
void main () {
Map<String, Map<String, int>> map1 = {“key” : {“eng_marks”: 80, “urdu_marks” : 70}};
}
For-in loop
List, Set & Map have a very close relationship with loop to manipulate/ play with data. The basic purpose of a loop is to repeat a work. This means when we intend to execute a code again and again. For example, if we need to find variables in a List, Set, or Map then it can be done through a loop. We can also apply operations for example add items, update items, and delete items on collections through the loop. The loop is to execute a block of code multiple times. Its format is as under:
void main() {
List list = [1, 3, 4, 5];
for (var item in list) {
print(item);
}
}
The items contained in a list will be executed at that time. We can also do operations in loops like:
void main() {
List list = [1, 3, 4, 5];
for (int item in list) {
print (item*10);
}
}
Try yourself in Dart Pad.
Understanding the concept of Map & Loop in Dart is very important in Flutter. So clear all these concepts before we jump further!!