dartdart2js

Dart - The setter 'someVar' is not defined for the class 'Map'


I'm trying to 'port' some script to the Dart. To learn how everything work etc. But there is a problem - in JavaScript we can set and get any variable in the object. In Dart we have a Map class. And I have no idea how to use it (there is not so many help from Dart API Reference). Currently I have:

Map settings;
//Then I get an dynamic result of a function that gives either null or object.
settings = result ?? {};
settings.someVar = 5;

And this code produces the following error:

"The setter 'someVar' is not defined for the class 'Map'."

Of course I can just 'invent' a new class Settings, but is there any other solutions?


Solution

  • With a Map, you get and put values with the [] and []= operators. So in this case you would use it like so;

    settings['someVar'] = 5;
    

    You can also use the addAll method;

    settings.addAll({'someVar': 5, 'someOtherVar': 10});
    

    Dart API References: operator [], operator []=, addAll