I'm trying to get several values from a list of maps but whenever I alter the new List values, it also alters the same values in the original list of maps.

How can I clone the list properly so this doesn't happen?

final List<Map> entries = [
  {'date': '2019-7-29', 'data': 85.0},
  {'date': '2019-8-2', 'data': 85.0},
  {'date': '2019-8-3', 'data': 85.0},
];

  List<Map> getSelected() {
    List<Map> obj = [];
    for (var i = 0; i < 2; i++) {
      obj.add(entries[i]);
      obj[i]['data'] = obj[i]['data'] * 2;
    }
    return obj;
  }

  selected = getSelected();

EDIT: This was suggested but doesn't appear to fix things. I guess because objects are passed by reference.

  List<Map> getSelected(List<Map> arr) {
    List<Map> obj = [];
    for (var i = 0; i < 2; i++) {
      obj.add(arr[i]);
      obj[i]['data'] = obj[i]['data'] * 2;
    }
    return obj;
  }

  selected = getSelected(entries);


Solution 1: Gürkan Sevinc

final List<Map> entries = [
{'date': '2019-7-29', 'data': 85.0},
{'date': '2019-8-2', 'data': 85.0},
{'date': '2019-8-3', 'data': 85.0},
];

List<Map> selected = new List<Map>(); entries.forEach((map) => { selected.add(new Map.from(map)) }); selected.forEach((map) => map["data"] = map["data"] * 2 );

would solve your issue.