Hi Guys, Welcome to Proto Coders Point, In this Article we will learn about map in dart programming language.
Dart Map
In Dart or any other programming language, A Map Object is a key-value pair to store string or any dynamic data.
Here, In dart map, A key & value can be of any datatype, It means that we can store any data type because a map in dart language is a dynamic collection of data/information.
In Flutter Dart, A map can be declared in 2 ways : –
- Using Map Literals.
- Using Map Constructor.
Declaring a map using Map Literals
To declare a map in dart you need to enclose the key-value pair in a curly bracket “{}”
Syntax
var identifier = { ‘key1′:’value1’ , ‘key2′:’value2’ , ‘key3′:’value3’, …….. };
Example
var usrMap = {"name": "Proto Coders Point ", 'Website': 'https://protocoderspoint.com/'}; print(usrMap);
output

To get/print particular key value then you can do it like this print ( usrMap[‘name’] ) this will print the value = Proto Coders Point.
Declartion of Map using Map Construtor
Using map constructor, A Map can be declared in 2 steps:
- Declare Map using it constructor
- Initilalize the map
Syntax
var identifier = new Map();
then, to initialize to maps with some key-value
identifier[key] = value
Example
void main() { var logincred = new Map(); logincred['username']='admin'; logincred['password']='admin@12334#'; print(logincred); }
output

Properties of Map in dart
So, to us map you need to import dart.core package
- Keys : This will return all the key in map object.
- Values : Wiil return all the value in map object.
- length : Return the number of key-value pair length/size.
- isEmpty : Will return true if Map object is empty.
- IsNotEmpty : Will return true if map value is not empty.
Function of Map dart
There are some comman task performed with Map in dart:
- addAll() : Used to add all key-value of some other Map Object.
- clean() : Remove all data from pair.
- remove() : Remove Specific date of key.
- foreach : used to iterate and read alll map entires.
addAdd() : Example
void main() { var usrMap = {"name": "Proto Coders Point ", 'Website': 'https://protocoderspoint.com/'}; var map2 = {'email': 'protocoders.come'}; usrMap.addAll(map2); print(usrMap); }
output

remove() : Example
Removes Specific data of key – values from map data, it accept a key which is needed to be removed.
void main() { Map m = { 'id': 01 ,'name': 'Rajat'}; print("Before removing : ${m}"); dynamic res = m.remove('name'); // here name is the key. print("removed data is : ${res}"); print("After removing ${m}"); }
output

foreach : Example
By using foreach function in Map you can easily iterate and read all the maps entries.
Syntax : Map.forEach(void f(K key , V value));
void main() { var userdata = { 'name': 'Proto Coders Point', 'Website': 'https://protocoderspoint.com/'}; userdata.forEach((k,v){ print(' ${k} ${v}'); }); }
output

Recommended Article
Dart data types – variable in dart
Flutter array – array in flutter
List in dart – convert list to set or vice versa