Dart – Convert Object to Map and Vice Versa

In this tutorial, we’re gonna look at several ways to convert Object to Map & Map to Object in Dart/Flutter.

Related Posts:
Dart/Flutter – Sort list of Objects
Dart/Flutter Constructors tutorial with examples
Dart/Flutter String Methods & Operators tutorial with examples
Dart/Flutter Future Tutorial with Examples

Dart/Flutter List Tutorial with Examples
Dart/Flutter Map Tutorial with Examples


Overview

Assume that we have a model class like this.

class Customer {
  String name;
  String email;
  int age;
  Customer(this.name, this.email, this.age);
  @override
  String toString() {
    return '{ ${this.name}, ${this.email}, ${this.age} }';
  }
}

What we’ll do is to convert Customer object to Map (<string, dynamic>) (Flutter/Dart) and vice versa as below.

  • key: Customer’s name
  • value: Customer’s email, age
// Customer
{ 'BezKoder', '[email protected]', 30 }
// Customer Map<string, dynamic>
{ name: BezKoder, email: [email protected], age: 30 }

Then we work with convert List of Objects to Map (<string, dynamic>) and vice versa.

// List of Customers
[{ Jack, [email protected], 23 }, { Adam, [email protected], 27 }, { Katherin, [email protected], 25 }]
// Map { name: {email, age} }
{Jack: {email: [email protected], age: 23}, Adam: {email: [email protected], age: 27}, Katherin: {email: [email protected], age: 25}}

Let’s go to the next sections.

Dart Object to Map

Let’s initialize an Dart Object first.

final customer = Customer('BezKoder', '[email protected]', 30);

We will convert this Customer object to Map with name, email, age as keys.

Map<String, dynamic> map = {
  'name': customer.name,
  'email': customer.email,
  'age': customer.age
};
print(map);

Output:

{name: BezKoder, email: [email protected], age: 30}

Dart List of Objects to Map

Before doing our work, we create a List with some items.

List<Customer> list = [];
list.add(Customer('Jack', '[email protected]', 23));
list.add(Customer('Adam', '[email protected]', 27));
list.add(Customer('Katherin', '[email protected]', 25));

Using Map.fromIterable()

We convert List<Customer> into Map using fromIterable() constructor.

var map = Map.fromIterable(list,
    key: (e) => e.name, value: (e) => {'email': e.email, 'age': e.age});
print(map);

We pass list as an Iterable for the first param.
Then, for each element of the iterable, the method computes key and value respectively.

Output:

{Jack: {email: [email protected], age: 23}, Adam: {email: [email protected], age: 27}, Katherin: {email: [email protected], age: 25}}

Using Iterable forEach() method

We can convert Dart List of Objects to Map in another way: forEach() method.

var map = {};
list.forEach((customer) =>
    map[customer.name] = {'email': customer.email, 'age': customer.age});
print(map);

This method is simple to understand.
We iterate over the list, for each item in the list, we add an entry (key,value) to the Map.

Output:

{Jack: {email: [email protected], age: 23}, Adam: {email: [email protected], age: 27}, Katherin: {email: [email protected], age: 25}}

Dart Map to Object

Let’s initialize a Dart Map first.

Map<String, dynamic> map = {
  'name': 'BezKoder',
  'email': '[email protected]',
  'age': 30
};

We will convert this Map<String, dynamic> to Customer object with name, email, age value from Map values. It is ease, we only need to use class constructor method and pass map[key] as the value.

final customer = Customer(map['name'], map['email'], map['age']);
print(customer);

Dart Map to List of Objects

Let’s initialize a Dart Map first.

Map<String, dynamic> map = {
  'Jack': {'email': '[email protected]', 'age': 23},
  'Adam': {'email': '[email protected]', 'age': 27},
  'Katherin': {'email': '[email protected]', 'age': 25}
};

Before that, initialize an empty list first.

List<Customer> list = [];

Using Map forEach() method

Now we convert our Map to List of Objects above using forEach() method.

map.forEach((k, v) => list.add(Customer(k, v['email'], v['age'])));
print(list);

In the code above, we create a new Customer object from each key-value pair, then add the object to the list.

Output:

[{ Jack, [email protected], 23 }, { Adam, [email protected], 27 }, { Katherin, [email protected], 25 }]

Using Iterable forEach() method

We can also convert a Dart Map to List of Objects using Iterable forEach() method instead.

We will apply forEach() to entries property of the map.
Every entry has a key-value field, we use them to create a new Customer object and add to the list.

map.entries.forEach(
    (e) => list.add(Customer(e.key, e.value['email'], e.value['age'])));
print(list);

Output:

[{ Jack, [email protected], 23 }, { Adam, [email protected], 27 }, { Katherin, [email protected], 25 }]

Using Iterable map() method

Another way to convert Map to a Dart List of Objects is to use Iterable map() method.

list = map.entries
          .map((e) => Customer(e.key, e.value['email'], e.value['age']))
          .toList();
print(list);

Each entry item of Map’s entries will be mapped to a Customer object with entry.key as customer.name and entry.value as email and age.

Then we convert the Iterable result to List using toList() method.

Output:

[{ Jack, [email protected], 23 }, { Adam, [email protected], 27 }, { Katherin, [email protected], 25 }]

Conclusion

Today we’ve known how to convert an Object to Map in Dart/Flutter, List of Objects to Map and vice versa.

For more details about these methods, also many interesting thing about Dart List and Map, please visit:
Dart/Flutter List Tutorial with Examples
Dart/Flutter Map Tutorial with Examples
Dart/Flutter – Sort list of Objects

Happy Learning! See you again.

Further Reading

Dart/Flutter Constructors tutorial with examples
Dart/Flutter String Methods & Operators tutorial with examples
Dart/Flutter Future Tutorial with Examples

Leave a Reply

Your email address will not be published.