Dart Map | RRTutors

Dart Maps

We can define maps by using  curly braces {}


 

main(List<String> args) {

  var map = {

    'key1': 'value1',

    'key2': 'value2',

    'key3': 'value3'

  };

 

  //Fetching the values

  print(map['key1']);    //Output: value1

  print(map['test']);    //Output: null

 

  //Add a new value

  map['key4'] = 'value4';

  

  //Length  

  print(map.length);

 

  //Check if a key is present 

  map.containsKey('value1');

 

  //Get entries and values

  var entries = map.entries;

  var values = map.values;

}

 

We can also define Map by constructor

 

main(List<String> args) {

  var squares = new Map();

  squares[4] = 16;

}

Advertisements