I like the .map((item){/*mapper*/})
that is on the Iterable
classes, but the Map
class doesn't appear to implement an analogous method. When I want to do the following, I have to create an blank instance and then forEach
over the existing Map
I want to map
to the new instance type:
void noSuchMethod(Invocation inv){
if(inv.isMethod){
var namedArguments = new Map<String, dynamic>();
inv.namedArguments.forEach((k, v){
namedArguments[MirrorSystem.getName(k)] = v;
});
return;
}
super.noSuchMethod(inv);
}
Is there a nicer way of mapping Map
s? It seems a litle odd that there is a forEach((v){})
for Iterable
s and a forEach((k, v){})
for Map
s but not a map((k, v){})
for Map
s.
Just for info
Now you can use a special method that is in every instance of the Map class.
final dict = { "id": 1, "name": "Alex", "isAdmin": true };
// A simple map, that contains a user's info
dict.map((key, value) => MapEntry(key, value.toString()));
// Here we can transform the key and the value of this map
print(dict);
// { "id": "1", "name": "Alex", "isAdmin": "true" }
// All fields are strings now
This should do what you want:
Map m = {'1': 1, '2':2 };
var newMap = new Map.fromIterable(m.keys, key: (k) => k , value: (v) => m[v] * 5 );
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