I am trying to use the "map" method/function on a Map object in Dart. (I know, it's a mouthful, and hard to search in google).
https://api.flutter.dev/flutter/dart-core/Map/map.html
It's essentially like List.map, generating a new Map by applying conversions to the keys and value pairs.
Why might you use this? Maybe you need to rename some keys in your Map object.
Example Code / Error
void main() {
Map myMap={"a":1, "b":2};
print(myMap);
print(myMap.map((k,v) {
k=k+k;
v=v*10;
return {k:v};
// I expect { aa:10,bb:20 }
}));
}
I get a compilation error:
Error: A value of type 'Map<dynamic, dynamic>' can't be returned from a function with return type 'MapEntry<dynamic, dynamic>'.
- 'Map' is from 'dart:core'.
- 'MapEntry' is from 'dart:core'.
return {k:v};
^
Error: Compilation failed.
Since I didn't find any examples online, I'm writing up the answer here.