Jesse
Jesse

Reputation: 47

Making a copy/clone of a "List<List<Map>>"

I'm trying to create a copy/clone of a "List<List<Map'>>".

So far I tried:

dataFTY2 = dataFTY.map((element)=>element).toList();
dataFTY2 = json.decode(json.encode(dataFTY));
dataFTY2 = List.from(dataFTY);

Nothing seems to work. Whenever I change the copy "dataFTY2", dataFTY changes as well. I need this to be a completely independent copy. Please help. I cant seem to figure this out, its driving me crazy.

More code added for reference.

List failureDetails = [];
    List trackIDs = [];
    List dateTime = [];
    var dataFTY2 = dataFTY.map((element) => element.map((ele) => Map.from(ele)).toList()).toList();

    // get historyData for each one and sort through "F"s and put them into the table in a row?
    for (var x in dataFTY2[4]) {
      trackIDs.add(x["track_id"]);
      dateTime.add(x["datetime"]);
    }
    List failuresOnly = List.filled(trackIDs.length, {}, growable: true);
    for (var i = 0; i < trackIDs.length; i++) {
      await fetchTrackIDTestDetails(context, trackIDs[i], dateTime[i], false);
      failureDetails.add(MyGlobals().getTestCodeDetailsData());
    }
    //filter out only "F"s
    for (var p = 0; p < failureDetails.length; p++) {
      for (var t in failureDetails[p][0]) {
        if (t["Status"] == "F") {
          //add it to list, if pass do nothing
          failuresOnly[p] = t;
        }
      }
    }
    //combine with FTY failure data, don't use new screen use old screen and toggle when pressed, add column on right side
    //dataFTY2 = MyGlobals().getFTYFailureMoreDetails();
    for (var i = 0; i < dataFTY2[4].length; i++) {
      dataFTY2[4][i]["TestCode"] = failuresOnly[i]["TestCode"];
      dataFTY2[4][i]["Status"] = failuresOnly[i]["Status"];
      dataFTY2[4][i]["TestValue"] = failuresOnly[i]["TestValue"];
      dataFTY2[4][i]["Lo_Limit"] = failuresOnly[i]["Lo_Limit"];
      dataFTY2[4][i]["Up_Limit"] = failuresOnly[i]["Up_Limit"];
      dataFTY2[4][i]["ProcTime"] = failuresOnly[i]["ProcTime"];
    }

Upvotes: 0

Views: 75

Answers (2)

jamesdlin
jamesdlin

Reputation: 90005

I find it more straightforward to use collection-for and the spread (...) operator:

void main() {
  var original = [
    [
      {'foo': 1, 'bar': 2},
      {'foo': 3, 'bar': 4},
    ]
  ];

  // Create a new List...
  var copy = [
    for (var sublist in original)
      // ... where each element is a new List...
      [
        for (var map in sublist)
          // ... where each element of the sublist is a new Map that
          // copies all entries from `map`.
          {...map},
      ],
  ];

  original[0][0]['foo'] = -1;
  print(original); // Prints: [[{foo: -1, bar: 2}, {foo: 3, bar: 4}]]
  print(copy); // Prints: [[{foo: 1, bar: 2}, {foo: 3, bar: 4}]]
}

Upvotes: 0

Yashawant
Yashawant

Reputation: 1150

You can use Map.from named constructor to clone the Map like this,

dataFTY2 = dataFTY.map((element) => element.map((ele) => Map.from(ele)).toList()).toList();

Upvotes: 1

Related Questions