Flutter Dart Convert Map to List & List to MapLast updated Oct 27, 2021 Show In this post we are going to learn how to convert Map to List and List to Map in Dart/Flutter. What is List in Dart? Dart represents arrays in the form ofListobjects. AListis simply an ordered group of objects List is classified in two ways
What is Map? The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection Map can be classified as below
Read dart list tutorial Let's check Suppose we have a class called User
Now let's convert List into Map and vice versa Convert Map to List in Dart/FlutterLet's initialize Map
We will convert thisMaptoListwithCustomer.namefrom a key andCustomer.agefrom a value.
Using Map forEach() methodNow we convert our Map to List above usingforEach()method
In the code above, we create a newUserobject from each key-value pair, then add the object to the user_list. Output:
Using Iterable forEach() methodWe can also convert a Dart Map to List using IterableforEach()method instead.
Output:
Using Iterable map() methodAnother way to convert Map to a Dart List is to use Iterablemap()method
Each entry item of Mapsentrieswill be mapped to a Userobject withentry.keyasuser.nameandentry.valueasuser.age.
Output:
Convert List to Map in Dart/FlutterNow Let's create a List with User Info
Using Map.fromIterable()We convertListintoMapusingfromIterable()constructor
Using Iterable forEach() methodWe can convert Dart List to Map in another way:forEach()method
Compare list and maps using DeepCollectionEquality collection class Conclusion: In this Dart tutorial we covered how to convert list to map and map to list in different ways Read More
|