You syntax is non-idiomatic but correct.
The code you posted works (assuming that StringStringMap1
and StringStringMap2
are Map
objects). I can successfully run the following code (as noted by ישו אוהב אותך, you need to wrap your code in a function, here main
as the entry point):
main() {
Map<String, Map> superMap = new Map();
superMap["subMap"] = new Map();
superMap["subMap"].addAll({1: 2});
superMap["subMap"].addAll({2: 2});
print(superMap);
}
I wanted to use this opportunity to share some idiomatic ways to write Dart code with you:
You can omit new
as it is not needed. Learn more.
Moreover, you can use Map
literals to construct your maps. Your empty map assignment at the start would look like this:
Map<String, Map> superMap = {};
- Furthermore, you can use the
final
keyword for variables that are not reassigned later:
final superMap = <String, Map>{}; // The compiler can infer the type from the assigned value.
With the Map
literal, you can also move your other assignments for your subMap
into the original assignment.
Additionally, you can spread your other maps and thus move the addAll
assignment to your original assignment as well.
The final result would look like the following. If that still does not work for you, I can come back to you:
main() {
final stringStringMap1 = {'1': '2'}, stringStringMap2 = {'2': '2'};
final superMap = <String, Map>{
'subMap': {
...stringStringMap1,
...stringStringMap2,
}
};
print(superMap);
}