What is Dart Map?

The Map in the Dart Programming language represents a collection of key-value pairs. We can retrieve a value from its associated key.

In any Programming Language, the Data Structure is a very important concept. Therefore we need to understand the basics of this key concept.

However, each key has exactly one value. In this section we will have an introductory note about the Dart Map.

Later, we will find more implementation in the Flutter Map section.

Why does the Dart Map play an important role in Flutter?

It is because the Map can be iterated. Now, there are different type of Maps. As a result, the order of iteration may change.

However, before the Dart Map, let us know about the Set first.

The Set is a kind of List. Although mainly unordered List. The order of the Set depends on its implementation.

The List is well indexed. Therefore, we can access by position. But in the Set, we cannot do that. Moreover, in the List, we can add duplicate items. The Set, however, does not allow that.

Syntax wise they are also different.

var lists = ['something'];
var sets = {'something'};

In Dart, a Set is an unordered collection of unique items.

Let us see a simple example of the Set.

main() {
  var fruitCollection = {'Mango', 'Apple', 'Jack fruit'};
  print(fruitCollection.lookup('Apple'));
}

//output
Apple

We can search the Set using the lookup() method. If the value is not there, it returns ‘null’.

main() {
  var fruitCollection = {'Mango', 'Apple', 'Jack fruit'};
  print(fruitCollection.lookup('Something Else'));
}

//output
null

Remember Syntax wise Set and Map look same.

How do I create a Dart map?

Watch the following code.

var myInteger = {};

It does not create an empty Set, but an empty Map.

The syntax for the Map literals is similar to that of for the Set literals.

Why does it happen? Because in the Dart, the Map literals comes first, the literal “{ }” is a default to the Map type.

We can prove this by a simple test.

main() {
  var myInteger = {};
  if(myInteger.isEmpty){
    print("It is a map that has no key, value pair.");
  } else print("It is a set that has no key, value pair.");
}

Watch the output:

//output

It is a map that has no key, value pair.

It means the map is empty.

If it was a set, we would have got the other output.

At present we need to remember, the Map is an object that associates keys and values.

How do Dart maps work?

Both keys and values can be of any “Type” of object.

Map<dynamic, dynamic> myProducts = {};
  myProducts['first'] = 'TV';
  myProducts[2] = 'Mobile';
  myProducts['third'] = 'Refrigerator';
  if (myProducts.containsValue('Mobile')) {
    print('Our products have ${myProducts[2]}');
  }

Each key occurs only once, but you can use the same value multiple times.

Dart support for the maps is provided by the Map literals and the Map type.

Maps support spread operators (… and …?). We have seen the three “…” in the List section.

 var myProductsOne = {'final': 3, ...myProducts};
  if (myProductsOne.containsValue('Mobile')) {
    print('Our products have ${myProductsOne[2]}');
  }
  if (myProductsOne.containsValue(3)) {
    print('Our products have ${myProductsOne['final']}');
  }
// Our products have Mobile
// Our products have 3

We can easily print any value of the Map using the key.

main() {
  var myProducts = {
    'first' : 'TV',
    'second' : 'Refrigerator',
    'third' : 'Mobile',
    'fourth' : 'Tablet',
    'fifth' : 'Computer'
  };
  print(myProducts['third']);
}
// output
// Mobile

The Dart understands that the ‘myProducts’ has the “Type” Map.

It is not mandatory that the Key and the Value should be of the same “Type”.

main() {
  Map<int, String> myProducts = {
    1 : 'TV',
    2 : 'Refrigerator',
    3 : 'Mobile',
    4 : 'Tablet',
    5 : 'Computer'
  };
  print(myProducts[3]);
}

The output is – mobile.

However, once we declare the “Type”, we cannot change that. It will throw error.

myProducts['1'] = 'First';
/// The argument type 'String' can't be assigned to the parameter type 'int'.

Can we add a Set type collection of value inside a Map?

Yes, we can. Consider this code:

main() {
  Set mySet = {1, 2, 3};
  var myProducts = {
    1 : 'TV',
    2 : 'Refrigerator',
    3 : mySet.lookup(2),
    4 : 'Tablet',
    5 : 'Computer'
  };
  print(myProducts[3]);
}
// output
// 2

In the above code, we have injected a collection of the “Type” Set into the Map.

In Flutter, the List and the Map will play a very important role.

Therefore, later we will learn how we can Map a List.

What is map data type in Dart?

In Dart, Map is the “Dictionary-like-Data-Type”. And it exists in the “key-value” form.

We can declare Map in two ways.

Using Map Literals like the following way.

var maps = {};

Or we can use the Map Constructors.

Here is a sample of a few code snippets that will give you an idea about the Set and the Map.

void main() {
  var fruitCollection = {'Mango', 'Apple', 'Jack fruit'};
  print(fruitCollection.lookup('Apple'));
  var anotherFruitCollection = {'Mango', 'Apple', 'Jack fruit'};
  print(anotherFruitCollection.lookup('Something Else'));
  var myInteger = {};
  if (myInteger.isEmpty) {
    print("It is a map that has no key, value pair.");
  } else
    print("It is a set that has no key, value pair.");

  var actors = {
    'first': 'De Nero',
    'second': 'Pacino',
    'third': 'Willis',
    'fourth': 'Morgan',
    'fifth': 'Hackman',
    1: 'Someone Else',
  };
  print(actors[1]);
  Map<int, String> maps = {};
  maps[1] = 'First';

  /// maps['1'] = 'First';
  /// The argument type 'String' can't be assigned to the parameter type 'int'.
  Map<dynamic, dynamic> myProducts = {};
  myProducts['first'] = 'TV';
  myProducts[2] = 'Mobile';
  myProducts['third'] = 'Refrigerator';
  if (myProducts.containsValue('Mobile')) {
    print('Our products have ${myProducts[2]}');
  }
  var myProductsOne = {'final': 3, ...myProducts};
  if (myProductsOne.containsValue('Mobile')) {
    print('Our products have ${myProductsOne[2]}');
  }
  if (myProductsOne.containsValue(3)) {
    print('Our products have ${myProductsOne['final']}');
  }
// Our products have Mobile
// Our products have Mobile
// Our products have Mobile
// Our products have 3
}

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Technical blog

Twitter

Comments

Leave a Reply