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!

4
  • 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 Lists contain. Commented Nov 26, 2021 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. Commented Nov 26, 2021 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. Commented Nov 26, 2021 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. Commented Nov 27, 2021 at 0:02

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

4 Comments

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 ;)
Please try the given code.
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!
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]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.