How to use maps in DART

In this article, i will go to explain what is maps and methods of maps.
  • 10923

Maps in DART

DART maps is an object that associates keys to value. In DART maps is an interface designed to manipulate a collection of keys which point to value.

  • Map literal

    In map literal every key must be a string. If you want every key does not a string , then use map constructor.

Example of map literal

var map={keys :values}; // map literal

  • Map constructor

    Map constructor can be string, a number or any other object.

Example of map constructor

var map = new Map();
map[1]="value";
map[2]="value";


 

You can add a new value in map like java script.
 

map["key"]="value";


Methods in maps

There are following maps method:

  • Length method

    By the help of this method you can find out maps length.

Syntax of length method
 

map.length;

  • Remove method

    If you want to remove of any value from map, then use remove().

Syntax of remove method
 

map.remove(key);

  • Copy method

    If you want to copies a map to another then use Map.from();

Syntax of copy method
 

var copymap=new Map.Fromcopy(mapname);

  • Empty method

    If you want to check a map is empty or not then use isEmpty().

Syntax of  empty  method
 

mapname.isEmpty();

 

  •  Containkey method:

               If you want to check a key is present in maps or not use containsKey();
 

Syntax of  containkey  method
 

mapname.containsKey();


Example of Maps

 

void main() {

var mapconst = new Map();

mapconst[1]="Google";

mapconst[2]="1.o.1";

var empty = {};

for(int i=1;i<=2;i++){

  print(mapconst[i]); 

}

var mapliteral = {"first": "c-sharpcorner", "second": "dotnetheaven"};

var values = mapliteral.getValues();

values.forEach((v) => print(v));

int len=mapliteral.length;

print("The length of mapliteral is :${len}");

print(mapliteral.remove('first'));

var copymap = new Map.from(mapconst);

for(int i=1;i<=2;i++){

  print(copymap[i]); 

}

print(mapconst.isEmpty());

print(empty.isEmpty());

print(mapliteral.containsKey("second"));

//mapliteral.containsKey("second");
}


Output:


mapsdart.jpg

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.