In Dart, is there any way to store a var as a map value and then reassign the map value/var to a new value (pointer, basically)?

I got this code:

characteristics =
    CharacteristicList.fromJson(importer.data['characteristic list'] ?? []);
starshipCharacteristics
    .fromJson(importer.data['starship characteristic list'] ?? []);
skills = SkillList.fromJson(importer.data['skill list'] ?? []);
obligations =
    ObligationList.fromJson(importer.data['obligation list'] ?? []);
motivations =
    MotivationList.fromJson(importer.data['motivation list'] ?? []);
...

which works ok, but not super pretty and easy to maintain. What I’d really like to do is have a map like this:

List<Map<String, dynamic>> dataList = [
  {
    'data': characteristics,
    'list': CharacteristicList.fromJson,
    'type': 'characteristic list'
  },
  ...
];

and then

dataList[i]['data'] =
    dataList[i]['list'](importer.data[dataList[i]['type']] ?? []);

That’s obviously not going to work because it’s updating the map, not characteristics itself.

Is there any possible way to achieve this (without a bunch of complicated added boiler code)?

  • Have only one source of truth, and update it there. Derive all the reshapings of that truth via currently executed code.

    – 

Leave a Comment