Add Your Heading Text Here

After the list data structure, we will learn one of the most important data structure i.e., Map.

Map also acts as the built in data type for dart programming language.

The map data structure has unique pattern of storing data in key-value pair.

The type of key and value in map can be of any data type.

Each key can occur only once(means every key should be unique), but you can use the same data for value multiple times. user can also provide any data to the value even NULL.

Maps can grow and shrink at runtime by adding the key-value pairs.

Maps in dart are supported using map literals(a notation for representing a fixed value) and map constructor.

Map using map literals

To declare a map, we need to enclose the key value pairs in between a pair of curly braces “{}”.

Syntax :

var variableMapName = { key:value, key2:value2, key3:value3 }

Map using map constructor

For ex :

void main() { 
  var dataMap = {'Name':'Bill','Surname':'Gates','Designation':'Developer'}; 
   print(dataMap); 
}

Output :

{ Name: Bill, Surname: Gates, Designation: Developer }

To declare a map using constructor, first create the map instance and then initialise the map objects.

Syntax :

var variableMap = Map(); // declare the map
variableMap [key] = value; // initialise the map

For ex :

void main() { 
   var deviceMap = {'Device':'Motorola','Model':'Moto G5','Manufacturer':'Motorola'}; 
   deviceMap['Brand'] = 'Google'; 
   print(deviceMap); 
}

Output :

{ Device: Motorola, Model: Moto G5, Manufacturer: Motorola, Brand: Google }

We can also define the data types for key-value pair at the time of map declaration.

For ex :

void main() { 
   var deviceMap = Map<String,int>(); 
   deviceMap['Age'] = 100; 
   deviceMap['Number'] = 200; 
   print(deviceMap); 
}

Output :

{Age: 100, Number: 200}

For above example, user can only pass the key of string data type and assign the value of Int data type. Otherwise, it will generate the compile time error as data type of key-value pair are defined already.

In this way you can define the data type for key value pair in dart maps.

Several built in properties and methods are defined to perform data related operations very quickly and efficiently.

Map Common Properties

For ex :

main(){
  var mapData=new Map<String,String>();
  mapData['King']="Arthur";
  mapData['Queen']="Alizabeth";
  mapData['Prince']="Harry";
  print(mapData);

  print(mapData.length);
  print(mapData.keys);
  print(mapData.values);
  print(mapData.isNotEmpty);
  print(mapData.isEmpty);
  print(mapData.runtimeType);
  print(mapData.hashCode);
}

Output :

{King: Arthur, Queen: Alizabeth, Prince: Harry}
3
(King, Queen, Prince)
(Arthur, Alizabeth, Harry)
true
false
JsLinkedHashMap
540333345

Map Common Methods

For ex :

main(){
  var mapData=new Map<int,String>();
  var mapData2={1:"A",2:"B",3:"C"};
  mapData[101]="Good";
  mapData[102]="Very Good";
  mapData[103]="Best";
  mapData[104]="Smarter";
  print(mapData);

  print(mapData.containsKey(111));
  print(mapData.containsValue("Best"));
  print(mapData.remove(104));
  print(mapData.toString());
  
  mapData.clear();
  mapData.addAll(mapData2);
  print(mapData);
  
  mapData.putIfAbsent(105, ()=>"hello");
  print(mapData);
}

Output :

{101: Good, 102: Very Good, 103: Best, 104: Smarter}
false
true
Smarter
{101: Good, 102: Very Good, 103: Best}
{1: A, 2: B, 3: C}
{1: A, 2: B, 3: C, 105: hello}

This all methods and properties describes that dart maps are very handy and dynamic data collection. So, user can anonymously use dart map for storing data in key-value pair.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons