Use Map Data in flutter | Add Data in Map, Delete, Update

In this blog, we will discuss Map. Also, we learn how to use Map with methods and functions in Dart/ Flutter.

The map is a collection of key: value pairs. The key is unique data in Map which holds a value. In Map, all data is covered by curly brackets "{ }". When you create Map then you can define the data type of Map, which type of data should be accepted by the map.


  1. Map<String, String> map {"one": "one"};
  2. Map<String, dynamic> map {1 : "one"};
  3. Map<int, String> map {1 : "1"};
  4. Map<int, int> map {};
  5. Map<dynamic, dynamic> map {};

There are three type of map, based on order of iteration.

1. Add Data in Map

When you already have Map and you want to add some other data. This dart has a special feature given below. Using this special feature you can add more data using for loop according your need.

Source Code
map.putIfAbsent(3 , () => "three");


You can add pair of more data in map using map.addAll();

Map map = {1: 'one', 2: 'two'};
map.addAll({3: 'three', 4: 'four', 5: 'five'});

{1: one, 2: two, 3: three, 4: four, 5: five}

2. Remove Data from Map

Map always store data in key : value format, you can remove data from map by value.

Map map = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'};

{1: 'one', 3: 'three', 4: 'four', 5: 'five'}

If you want to remove data with the condition, like string name, number, match name, etc. use the below example.

Map map = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'};
map.removeWhere((k, v) => v.startsWith('f'));

{1: one, 2: two, 3: three}

Sometimes we need to clear all map, you have to write just one line map.clear().



3. Update value in Map

Update data in Map using key in flutter/ Dart, for this we have a method for update map.

Map map = {1: 'one', 2: 'two'};
map.update(2, (v) {
return 'TWO';

{1: 'one', 2: 'TWO'}

You can also update data using square brackets [ ] of map.

Map map = {1: 'one', 2: 'two'};
map[1] = "ONE";

{1: 'ONE', 2: 'two'}

Once you try to update information within the outline by the update method, but you are not confirm that these values available on the map or not. For this, you'll use the update method with ifAbsent for new data entry.

Map map = {1: 'one', 2: 'two'};
map.update(3, (v) => 'THREE', ifAbsent: () => 'AddedThree');

{1: 'one', 2: 'two', 3: 'AddedThree'}

We have discussed Add, Delete, and update methods in Map flutter/ Dart. If you have any queries about this concept of Map, then please ask in the comment section. We will deliver a response soon.

Next Post »