Home > Blockchain >  strange result in dart list how to solve this
strange result in dart list how to solve this

Time:01-26

i have the following

List list1 = [];
List list2 = [];

list2 = list1 ;

list1.add(1);

print(list1); // outputs [1] 
print(list2); // outputs [1] WHY?

i only change list1 .. why list2 is always be the same ..

sometimes in my app i need to make a list == another .. and this is great .. but once i make it they always be equals to each other even if i make a change to one of them

CodePudding user response:

Assign list copy with List.from constructor method:

list2 = List.from(list1);

More explanation of pointers and how it works you can find at my answer here.

  •  Tags:  
  • dart
  • Related