list.map flutter
Dart: Converting a List to a Map and Vice VersaLast updated on August 19, 2021 A Goodman Loading... Loading... Post a comment Show 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 MapsUsing List.asMap() methodThis 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() { MapOutput: {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 methodAdvertisementsThis 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() { MapOutput: { 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() { MapConverting Maps to ListsDart supports several tools that can help us get the job done with ease. Using Map.entriesExample: // kindacode.com MapAdvertisements Output: [{key1: value1}, {key2: value2}, {key3: value3}, {key4: value4}] [value1, value2, value3, value4] [key1, key2, key3, key4]Using Map.forEach methodExample: // 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]ConclusionWeve 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:
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 ArticlesDart: 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 |