Map string dynamic to list flutter
The article Parsing Complex JSON in Flutter is very good, but I was looking for something shorter, a quick reference with examples. If I have a simple JSON string like this: How do I convert it to a Dart object? Or a Dart object to JSON? Import the builtin convert library, which includes the jsonDecode() and jsonEncode() methods:
In Dart and flutter, this tutorial explains how to convert List A dynamic type is a primitive type that may hold any dynamic value, such as an integer, a string, or a double. Because a string is a primitive type that stores a collection of characters, automated conversion to/from dynamic is not possible. You can check on How to convert dynamic to string vice versa or vice versa in Dart and flutter. How to Convert List of String into List of Dynamic type in DartIn dart and flutter, this example converts a list of strings to a list of dynamic.
void main() { List<String> numbers = <String>['one', 'two', "four"]; print(numbers.runtimeType); List<dynamic> dynamicValues = List<dynamic>.from(numbers); print(dynamicValues.runtimeType); print(dynamicValues); } Output: JSArray How to parse List of Dynamic into List of String type in DartIn dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list. void main() { List<dynamic> numbers = <dynamic>['one', 'two', "four"]; print(numbers.runtimeType); final List<String> strs = numbers.map((e) => e.toString()).toList(); print(strs.runtimeType); print(strs); } Output: JSArray ConclusionLearned how to parse and convert a list of strings into a list of dynamic and vice versa. In this tutorial, we’re gonna look at several ways to convert Map to List & List to Map in Dart/Flutter using List & Map methods. Related Posts: – Dart/Flutter List Tutorial with Examples OverviewAssume that we have a model class like this. class Customer { String name; int age; Customer(this.name, this.age); @override String toString() { return '{ ${this.name}, ${this.age} }'; } }What we’ll do is to convert List
Let’s go to the next sections. Convert Map to List in Dart/FlutterLet’s initialize a Dart Map first. Map map = {'Jack': 23, 'Adam': 27, 'Katherin': 25};We will convert this Map to List Using Map forEach() methodNow we convert our Map to List above using forEach() method. map.forEach((k, v) => list.add(Customer(k, v))); 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, 23 }, { Adam, 27 }, { Katherin, 25 }]Using Iterable forEach() methodWe can also convert a Dart Map to List using Iterable forEach() method instead. map.entries.forEach((e) => list.add(Customer(e.key, e.value))); print(list);We apply forEach() to entries property of the map. Output: [{ Jack, 23 }, { Adam, 27 }, { Katherin, 25 }]Using Iterable map() methodAnother way to convert Map to a Dart List is to use Iterable map() method. list = map.entries.map((e) => Customer(e.key, e.value)).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 customer.age. Output: [{ Jack, 23 }, { Adam, 27 }, { Katherin, 25 }]Convert List to Map in Dart/FlutterBefore doing our work, we create a List with some items. List list = []; list.add(Customer('Jack', 23)); list.add(Customer('Adam', 27)); list.add(Customer('Katherin', 25));Using Map.fromIterable()We convert List We pass list as an Iterable for the first param. Output: {Jack: 23, Adam: 27, Katherin: 25}Using Iterable forEach() methodWe can convert Dart List to Map in another way: forEach() method. var map2 = {}; list.forEach((customer) => map2[customer.name] = customer.age); print(map2);This method is simple to understand. Output: {Jack: 23, Adam: 27, Katherin: 25}ConclusionToday we’ve known how to convert a Map to List in Dart/Flutter using Map.fromIterable() or Iterable forEach() method. We also do the opposite thing: convert a List to Map using Map.forEach(), Iterable forEach() or map() method. For more details about these methods, also many interesting thing about Dart List and Map, please visit: Happy Learning! See you again. Further Reading
– Dart/Flutter Constructors tutorial with examples
In this post, we will understand the Flutter concepts related to Map Map { "name": "Vitalflux.com", "domain": ["Data Science", "Mobile", "Web"], "noOfArticles": [ {"type": "data science", "count": 50}, {"type": "web", "count": 75} ] } Above is an example of JSON object comprising of three keys such as name with value of type String, “domain” with value of type List Such JSON objects can easily be read with Map There are various ways in which one could work with Map
The following will be required to be done in case the Map
The following code represents the parse method which is used to create a Dart object. class PortalInfoWidget extends StatelessWidget {
Map Note the fromJson factory method called on the PortalInfo (Dart object) which takes the Map Here is the code for PortalInfo object. class PortalInfo {
final String name;
final List Running the app will create a view like the following: |