Dart: Converting a List to a Map and Vice Versa
Last updated on August 19, 2021 A Goodman Loading... Loading... Post a comment
Dart
[ 53 Articles]
Sorting Lists in Dart and Flutter [5 Examples]
May 21, 2021
Flutter & Dart: ENUM Example
July 18, 2021
Base64 encoding and decoding in Dart [and Flutter]
February 12, 2022
How to clone a List or Map in Flutter/Dart [4 methods]
February 12, 2022
Flutter & Dart: fold[] method examples
July 27, 2021
2 Ways to Create Multi-Line Strings in Dart
May 17, 2021
How to check your Flutter and Dart versions
March 31, 2021
Flutter & Dart: 3 Ways to Generate Random Strings
November 24, 2021
More
In this article, we will go over a couple of different ways to convert a list to a map as well as turn a map into a map in Dart. Without any further ado, lets dive right in.
Table of Contents
- Turning Lists into Maps
- Using List.asMap[] method
- Using Map.fromIterable or List.forEach method
- Converting Maps to Lists
- Using Map.entries
- Using Map.forEach method
- Conclusion
Turning Lists into Maps
Using List.asMap[] method
This method will return an immutable map from a given list. The returned map uses the indexes of the list as keys and the corresponding objects as values.Advertisements
Example:
// kindacode.com List list = ['orange', 'banana', 'apple', 'papaya']; void main[] { Map map = list.asMap[]; print[map]; print[map[0]]; print[map[1]]; }Output:
{0: orange, 1: banana, 2: apple, 3: papaya} orange bananaIf you try to modify the map, an error will occur. If you need to update the map without errors, see other approaches.
Using Map.fromIterable or List.forEach method
AdvertisementsThis approach is much more flexible than the above one. You can decide how the keys and values of the map look like.
Example
Lets say we have a list of users and we want to create a map from this list. This map will use the ids of the users as the keys.
Advertisements
Using Map.fromIterable:
// kindacode.com List list = [ {'_id': 'u1', 'name': 'John Doe', 'age': 30}, {'_id': 'u2', 'name': 'Robbin', 'age': 40}, {'_id': 'u3', 'name': 'Tom', 'age': 50} ]; void main[] { Map map = Map.fromIterable[list, key: [item] => item['_id'], value: [item] => {'name': item['name'], 'age': item['age']}]; print[map]; }Output:
{ u1: {name: John Doe, age: 30}, u2: {name: Robbin, age: 40}, u3: {name: Tom, age: 50} }You can also use the List.forEach method to get the same results, as shown in the code snippet below:
// main.dart List list = [ {'_id': 'u1', 'name': 'John Doe', 'age': 30}, {'_id': 'u2', 'name': 'Robbin', 'age': 40}, {'_id': 'u3', 'name': 'Tom', 'age': 50} ]; void main[] { Map map = {}; list.forEach[[item] { map[item['_id']] = {'name': item['name'], 'age': item['age']}; }]; print[map]; }Converting Maps to Lists
Dart supports several tools that can help us get the job done with ease.
Using Map.entries
Example:
// kindacode.com Map map = { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4' }; void main[] { List list = []; // This list contains both keys and values List listValues = []; // This list only keeps values List listKeys = []; // This list only contains keys map.entries.map[[e] => list.add[{e.key: e.value}]].toList[]; print[list]; map.entries.map[[e] => listValues.add[e.value]].toList[]; print[listValues]; map.entries.map[[e] => listKeys.add[e.key]].toList[]; print[listKeys]; }Advertisements
Output:
[{key1: value1}, {key2: value2}, {key3: value3}, {key4: value4}] [value1, value2, value3, value4] [key1, key2, key3, key4]Using Map.forEach method
Example:
// kindacode.com Map map = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}; void main[] { List list = []; List keys = []; List values = []; map.forEach[[key, value] { // List of keys and values list.add[{key: value}]; // List of keys keys.add[key]; // List of values values.add[value]; }]; print[list]; print[keys]; print[values]; }Output:
[{key1: value1}, {key2: value2}, {key3: value3}] [key1, key2, key3] [value1, value2, value3]Conclusion
Weve walked through a few examples of converting a list to a map and a map to a list in Dart. If youd like to explore more new and interesting features of modern Dart and Flutter, take a look at the following articles:
- Dart: How to Add new Key/Value Pairs to a Map
- Using Cascade Notation in Dart and Flutter
- Dart: Convert Timestamp to DateTime and vice versa
- Dart & Flutter: 2 Ways to Count Words in a String
- Sorting Lists in Dart and Flutter [5 Examples]
Advertisements
Share Tweet Telegram
Subscribe
Notify of
I allow to use my email address and send notification about new comments and replies [you can unsubscribe at any time].
Label
{} [+]
Name* Email*
Label
{} [+]
Name* Email*
0 Comments
Inline Feedbacks
View all comments
Related Articles
Dart: Convert Class Instances [Objects] to Maps and Vice Versa
January 7, 2022
How to Flatten a Nested List in Dart
December 3, 2021
Inheritance in Dart: A Quick Example
December 3, 2021
Flutter & Dart: 3 Ways to Generate Random Strings
November 24, 2021
Flutter & Dart: Displaying Large Numbers with Digit Grouping
November 7, 2021
Using Static Methods in Dart and Flutter
October 1, 2021
Prevent VS Code from Auto Formatting Flutter/Dart Code
August 24, 2021
Dart: Checking whether a Map is empty
August 17, 2021
Dart: Checking if a Map contains a given Key/Value
August 17, 2021
Dart: How to Add new Key/Value Pairs to a Map
August 17, 2021
Dart: How to Update a Map
August 17, 2021
Dart: How to remove specific Entries from a Map
August 17, 2021