How to use maps in DART
In this article, i will go to explain what is maps and methods of maps.
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.
Methods in maps
There are following maps method:
Syntax of length method
Syntax of remove method
Syntax of copy method
var copymap=new Map.Fromcopy(mapname);
|
Syntax of empty method
If you want to check a key is present in maps or not use containsKey();
Syntax of containkey method
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:
Ask Your Question
Got a programming related question? You may want to post your question here