online software programming courses

Understanding Dart Map and List for Flutter Development

All About Map

In flutter and dart Map is a collection. It contians data like below

Map and List are one most the most foundamentals for dart programming and if you want to learn flutter well, you must master Map and List.

Map could be used from basic data manupulation, routing and sqflite data storage.

"name":"Dastagir Ahmed",

If you see the carefully, you will see that, it exactly look like json format in Javascript. 

If you are familiar with Javascript, it would be very easy to understand the concept. JSON format is for storing keys with values. 

In the above example name, age and country are our keys and Dastagir Ahmed, age and Bangladesh is our values. 

So Map in Dart is the same. So What Map does in dart?

Stores information (like data, object) in key and value pair

So whenever you see data with curly braces (most outer), you need to recognize it as a Map.

There are at least three different ways to create Map object in flutter

First way

var mapObj1= {
"name":"Dastagir Ahmed",


First way is the most common way. Just wrap your data within curly braces in key and value pair. Take care of the keys in double quotation.

If you see the printed value you will see 

{name: Dastagir Ahmed, age: 34, country: Bangladesh}

As you see, printed values are also wrapped in curly braces.

Second way

var mapObj2 = new Map();
mapObj["name"]="Dastagir Ahmed";


and if you print the result you will see 

{name: Dastagir Ahmed, age: 34, country: Bangladesh}

Which is exaclty the same the first way and printed results are same and wrapped in curly braces. It's very important to notice.

The third way

var mapObj3 = Map<String, dynamic>();
mapObj3["name"]="Dastagir Ahmed";


This way, you explicitly mention the data type for key and value. In our case for keys we have used string format. And keys are always in string format anyway. For values we have used dynamic type since they could be integer, string or even object type. Of course for values you can any specific type if you are pretty sure about data format.

And see the printed value

{name: Dastagir Ahmed, age: 34, country: Bangladesh}

Accessing Map data

To access them you can use with the keyword "keys" or you can explicitly mention the key if you want to retrieve any specific value for a key.

Using keys


and you will see the result as below

{name: Dastagir Ahmed, age: 34, country: Bangladesh}
{name: Dastagir Ahmed, age: 34, country: Bangladesh}
{name: Dastagir Ahmed, age: 34, country: Bangladesh}

Explicity mention the keys


The results are 

Dastagir Ahmed

There are other ways to access them. You can use forEach method to access the keys. See the example below

  mapObj1.forEach((key, value){
// notice the entries property

So map object allows you to access them using forEach but it has also varieties. As you can see it's entries property as well.

The result is 


You can use them to access the values as well. See the example below

  mapObj1.forEach((key, value){
// notice the entries property

The result is

Dastagir Ahmed

You can also access them using .map method{

If you use .map method you must need to use .toList() to make map iterable. Otherwise it won't work. Anyway, the printed result is 

MapEntry(name: Dastagir Ahmed)
MapEntry(age: 36)
MapEntry(country: Bangladesh)

At the same time you can use the key or value to retrieve keys and values{


Excessing using index

You can also access data by index. To do that, you would do like below


It will print "name" on the console for the keys. You can also access the value using elementAt. 


It will print Dastagir Ahmed on the console.

See how to use them in a project

Delcare map of objects

You can create a class model and declare map based on that class object.

class Category {
  final String name;
  final Color color;

  Category(, this.color);

Here category is class, which has two properties name and color. 

Now we can create a map based on that. This map also would be in key and pair value. 

 var _categories = {
    Category("Apple", false,
    Category("Orange", false,
    Category("Banana", Colors.yellow): false,

Here _categories is a map and it's key and pair are object and boolean. See the practical Example how to use map


All About List

List has a special trait. Lists are wrapped with third brackets or the square brackets. See an example below

var ageList = ["35", "39", "27"];

And if you do 


You will get

[35, 39, 27]

You can access them individually by index. That's very different from Map. In Map object, you can access them using index.

For the list object you can do 


And you will get 35, 39, 27


If you have a model which creates objects for your application and stores in the database, you need to convert to Map or JSON object before you save them into the database.

Otherwise you won't be able to retreive information from object.  

If your model is responsible for creating data object, then you should have a method, that converts that data info Map format (like key and value pair)

class User{
  String name;
  int age;
  String location;
  User(, this.age, this.location);
  Map<String, dynamic> toJson(){
    return {