Debug School

rakesh kumar
rakesh kumar

Posted on

Dart Programming - Map

  1. The Map object is a simple key/value pair.
  2. Keys and values in a map may be of any type.
  3. A Map is a dynamic collection. In other words
  4. Maps can grow and shrink at runtime.

Maps can be declared in two ways −

Using Map Literals
Using a Map constructor

Declaring a Map using Map Literals
To declare a map using map literals, you need to enclose the key-value pairs within a pair of curly brackets "{ }".

Here is its syntax −

var identifier = { key1:value1, key2:value2 [,…..,key_n:value_n] }
Declaring a Map using a Map Constructor
To declare a Map using a Map constructor, we have two steps. First, declare the map and second, initialize the map.

The syntax to declare a map is as follows −

var identifier = new Map()
Now, use the following syntax to initialize the map −

map_name[key] = value
Example: Map Literal


void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   print(details); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

{Usrname: tom, Password: pass@123}
Example: Adding Values to Map Literals at Runtime

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   details['Uid'] = 'U1oo1'; 
   print(details); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

{Usrname: tom, Password: pass@123, Uid: U1oo1}
Example: Map Constructor

void main() { 
   var details = new Map(); 
   details['Usrname'] = 'admin'; 
   details['Password'] = 'admin@123'; 
   print(details); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

{Usrname: admin, Password: admin@123}
Note − A map value can be any object including NULL.

Dart Programming - Map Property Keys

Syntax
Map.keys
Example

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   print(details.keys); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

(Usrname, Password)

Dart Programming - Map Property Values

Syntax
Map.values
Example

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   print(details.values); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

(tom, pass@123)

Dart Programming - Map Property Length

Returns the size of the Map.

Syntax
Map.length
Example

void main() {
   var details = {'Usrname':'tom','Password':'pass@123'};
   print(details.length);
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

2

Dart Programming - Map Property isEmpty

Returns true if the Map is empty.

Syntax
Map.isEmpty
Example

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   print(details.isEmpty); 
   var hosts = {}; 
   print(hosts.isEmpty); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

false
true

Dart Programming - Map Property isNotEmpty

Returns true if the Map has at least one item.

Syntax

Map.isNotEmpty
Example

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   print(details.isNotEmpty); 

   var hosts = {}; 
   print(hosts.isNotEmpty); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

true
false

Dart Programming - Map.addAll() Function

The** Map.addAll()** function adds all key-value pairs of other to this map.

Syntax
Map.addAll(Map other)
Parameter
other − represents a key value pair.

Return Type − void

Example

void main() { 
   Map m = {'name':'Tom','Id':'E1001'}; 
   print('Map :${m}'); 

   m.addAll({'dept':'HR','email':'tom@xyz.com'}); 
   print('Map after adding  entries :${m}'); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

Map : {name: Tom, Id: E1001}
Map after adding entries : {name: Tom, Id: E1001, dept: HR, email: tom@xyz.com}

Dart Programming - Map.clear() Function

Removes all pairs from the map.

Syntax
Map.clear()
Return Type − void

Example

void main() { 
   Map m = {'name':'Tom','Id':'E1001'}; 
   print('Map :${m}'); 

   m.clear(); 
   print('Map after invoking clear()  :${m}'); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

Map :{name: Tom, Id: E1001}
Map after invoking clear() :{}

Dart Programming - Map.remove()

Removes key and its associated value, if present, from the map. The function also returns the value associated with the key.

Syntax
Map.remove(Object key)
Parameters
Keys − identifies the entry to be deleted.

Return Type − Returns the value corresponding to the specified key.

Example

void main() { 
   Map m = {'name':'Tom','Id':'E1001'}; 
   print('Map :${m}'); 

   dynamic res = m.remove('name'); 
   print('Value popped from the Map :${res}'); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

Map :{name: Tom, Id: E1001}
Value popped from the Map :Tom

Dart Programming - Map.forEach() Function

Applies the specified function on every Map entry. In other words, forEach enables iterating through the Map’s entries.

Syntax
Map.forEach(void f(K key, V value));
Parameters
f(K key, V value) − Applies f to each key-value pair of the map.

Calling f must not add or remove keys from the map

Return Type − void.

Example

void main() { 
   var usrMap = {"name": "Tom", 'Email': 'tom@xyz.com'}; 
   usrMap.forEach((k,v) => print('${k}: ${v}')); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

name: Tom
Email: tom@xyz.com

Full Summary

Map – Properties
The Map class in the dart:core package defines the following properties −

Sr.No Property & Description
1 Keys
Returns an iterable object representing keys

2 Values
Returns an iterable object representing values

3 Length
Returns the size of the Map

4 isEmpty
Returns true if the Map is an empty Map

5 isNotEmpty
Returns true if the Map is an empty Map

Map - Functions
Following are the commonly used functions for manipulating Maps in Dart.

Function Name & Description
Enter fullscreen mode Exit fullscreen mode

1 addAll()
Adds all key-value pairs of other to this map.

2 clear()
Removes all pairs from the map.

3 remove()
Removes key and its associated value, if present, from the map.

4 forEach()
Applies f to each key-value pair of the map.

Top comments (0)