1

I have a list of objects that hold some user input.

What I want to achieve: Duplicating the list before resetting the objects to their default values, so I don't lose the information.

The problem: No matter what I try, whenever I modify objects in list_1, the objects in list_2 are being modified as well; overwriting the data I want to keep that way.

Attempts at solving it:

I tried declaring the second lists in all kinds of ways:

list_2 = list_1;
list_2 = List.of(list_1);
list_2 = [...list_1);
list_2 = list_1.toList();

No luck. I then tried this:

list_2=[];
for (var i in list_1){
  list_2.add(i);}

Still, the same behaviour. If I modify a value of an object in list_1, the corresponding object in list_2 is changed as well.

I'm confused. Am I only creating new references to the objects, but not actually multiplying them? How would I go about changing that? Is something else going on? THANKS!

Joe
  • 311
  • 3
  • 17
  • Your problem is that **there is no general way of making a copy of an object in Dart**. `List.of(list_1)`, `[...list_1]`, and `list1.toList()` will all duplicate `list_1` but *not* the contained objects. An answer to your question **depends on what those objects are**, and you haven't specified what objects your `List`s contain. – jamesdlin Nov 26 '21 at 23:37
  • The objects are instances of a class I created. They have two properties; one to store a String (user input from a TextField) and the other stores an integer that marks, if the user marked a task as done. My idea was to use that object to keep information when resetting stuff. >> not sure if you can tell ;)...but I'm super new to this; so this is for a training project I came up with for myself ...precisely to encounter issues like this. – Joe Nov 26 '21 at 23:42
  • You will need to iterate over the original `list_1` and build a new `List`, invoking an appropriate constructor of your class for each element. Adding a `clone()` method to your class can make it easier, but you would need to take care to override it appropriately in any derived classes. – jamesdlin Nov 26 '21 at 23:46
  • Thanks! I've never heard of the clone() thingy, so I'll need to look into it....but even if I decide that's too much of a tangent for now ... I think I can come up with a way to call my class, create new objects and put them in a new list. So it's a way forward and I understand the root cause of the issue...which relly left me baffled. Thanks again. – Joe Nov 27 '21 at 00:02

2 Answers2

1

Try this one:

Created a demo list:

  List<Status> statuses = <Status>[
    Status(name: 'Confirmed', isCheck: true),
    Status(name: 'Cancelled', isCheck: true),
  ];
List<Status> otherStatuses = statuses.map((status)=>Status(name:status.name, isCheck:status.isCheck)).toList()
dartKnightRises
  • 815
  • 15
  • 26
  • myList = List.from(mynewlist); > tried it; same thing. Thanks for answering anyways though! ... at least I'm leanring A LOT OF WAYS to create lists from lists today ;) – Joe Nov 26 '21 at 23:14
  • Please try the given code. – dartKnightRises Nov 26 '21 at 23:29
  • Not quite sure what this is doing. I create a list of maps and then map that list onto another list? Can you give me a hint as to why that helps here? It's late...I'll nap and look at it again tomorrow. Thanks! – Joe Nov 26 '21 at 23:36
  • Follow these references: https://stackoverflow.com/questions/54154230/how-do-i-create-a-copy-of-listt-in-dart-when-t-is-object-see-below-code, https://stackoverflow.com/questions/50460367/how-to-copy-listt-in-dart-without-old-reference – dartKnightRises Nov 26 '21 at 23:42
0

this happen to me while aago with objects

I fixed it by converting the object to json format then asign the new object from the json data with the method from the model fromJson(json) that worked fine

in case of the list there is an easier way

main() {
 List<int> x = [1, 2];

 List<int> y = [];

 x.map((e) => y.add(e)).toList();

 print(x.toString()); //[1, 2]
 y[0]++;
 print(y.toString()); //[2, 2]
 print(x.toString()); //[1, 2]
}