A common thing I do in my web application is request a resource from the server and handle it as a Map in dart.
import 'dart:convert';
const String sampleJSON = '''
{
"member": {
"meaningOfLife": 42
}
}
''';
Map<String, dynamic> getResource() {
// do some magic
return JSON.decode(sampleJSON);
}
I live with the assumption that all keys in a JSON decoded Map will be Strings but obviously I have no clue of the value's type. In checked mode this worked fairly well.
Analysis in strong mode to the code above will tell me: Unsound implicit cast from dynamic to Map<String, dynamic>
Question:
What is a good strategy to handle such cast warnings?
Questionable options 1:
Map getResource() {
// do some magic
return JSON.decode(sampleJSON);
}
Later this could be a problem: Iterable<String> keys = getResource().keys
will give a warning.
Questionable option 2:
Map<String, dynamic> getResource() {
// do some magic
return new Map<String, dynamic>.from(JSON.decode(sampleJSON));
}
Does it not degrade performance much? And I will still get a warning for Map<String, dynamic> meaning = getResource()["member"];
Thanks for the advice.