3

I'm trying to create Map object.

I'm new to dart and due to this, I'm trying it just like I would do it in JS, but this seems to be incorrect.

Map<String, Map> superMap = new Map();
superMap["subMap"] = new Map();
superMap["subMap"].addAll(StringStringMap1);
superMap["subMap"].addAll(StringStringMap2);

This results in:

Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
Try adding the name of the type of the variable or the keyword 'var'.
superMap["subMap"] = new Map();
^^^^^^^^
Error: Expected ';' after this.
superMap["subMap"] = new Map();
^^^^^^^^
Error: 'superMap' is already declared in this scope.
superMap["subMap"] = new Map();
^^^^^^^^
etc...
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
vexa
  • 31
  • 1
  • 2

2 Answers2

2

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);
}
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
1

There is no problem with your following code:

Map<String, Map> superMap = new Map();
superMap["subMap"] = new Map();
superMap["subMap"].addAll(StringStringMap1);
superMap["subMap"].addAll(StringStringMap2);

but it works when I change the addAll to this:

var maps = {"key1": "value1", "key2": "value2"};
superMap["subMap"].addAll(maps);
superMap["subMap"].addAll(maps);

So, check if your StringStringMap1 and StringStringMap2 is correct.

But I suspect that you haven't create the code inside a function. So, try your code inside a main, something like this:

main() {

    Map<String, Map> superMap = new Map();
    superMap["subMap"] = new Map();
    superMap["subMap"].addAll(StringStringMap1);
    superMap["subMap"].addAll(StringStringMap2);

}

please be noted that in Dart, the entry point for the code is the main() function. You can't executed the line outside the function like in Javascript.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96