I've been searching for a while without success. I am trying to convert a Map to a List (discarding key).
Sure, I can do
var addictionList = new List<AddictionDay>();
dataMan.forEach((String date, AddictionDay day) {
addictionList.add(day);
});
but is there a more straight approach, like
var addictionList = dataMan.toList();
?
map
function of Map doesn't seem to help a lot, because it accepts only MapEntry<K2, V2>
as a param so it's a no go. Pretty surprised nobody asked this question before.
I was also thinking about new class extending Map, but right now I am just looking into some native way to do this, if it exists.
Map class has the entries property which returns the map entries as an iterable on which you can use the map function to generate the list of items you need.
var addictionList = dataMan.entries.map((MapEntry mapEntry) => mapEntry.value).toList();
mapEntry.key
is the type of String
and mapEntry.value
is the type of AddictionDay
.
Do you need this or something more complicated?
var _map = {1: 'one', 2: 'two', 3: 'three'};
var values =_map.values.toList();
print(values);
Result:
[one, two, three]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With