I am trying to copy values of one list to another, I use three buttons 1st one to append a value to mylist, second one to clear the mylist, 3rd button to copy values from mynewlist to mylist.
i tried this
List<String> mylist = [
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
];
List<String> mynewlist = [
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
];
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 1st');
print(mylist.length);
print(mynewlist.length);
mylist.add('sdsds');
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 2nd');
print(mylist.length);
print(mynewlist.length);
//after i set mylist = mynewlist; when i click this button it clears the old and new list.
mylist.removeRange(0, mylist.length);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 3rd');
print(mylist.length);
print(mynewlist.length);
mylist = mynewlist;
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
],
),
)
On the initial time it works perfectly the second time i click the second button it clears the mylist and mynewlist.
How can i copy the values of second list without clearing the new new list
The first one is using List. addAll(). Pass the list as argument, to addAll() method, which you would like to append to this list. The second way is traversing through the second list and adding the element during iteration to the first list.
Use myList = List.from(mynewlist);
instead of mylist = mynewlist;
Thats because you copied the object references (mylist = mynewlist) and not the content of the list. So after the first click, mylist has a reference to the same object in memory as mynewlist. So any operation on one of them, affect both.
To solve your problem you need to keep the object references intact and just copy around the contents of these lists.
var YOURCOPY = YOURLIST.map((v) => v).toList();
You can also call method:toList() on any iterables (in this case List) which you want to create a copy of and not a reference.
If you have a list of classes, make sure to clone/copy each class. Otherwise, if you change objects in the original list, it will also change in the new one. Here is one way to prevent it:
Add a clone()
function to your class
class RandomObject {
RandomObject(this.x, this.y);
//add clone function to your class:
RandomObject.clone(RandomObject randomObject): this(randomObject.x, randomObject.y);
int x;
int y;
}
To copy, map through each element and clone it
final List<RandomObject> original = [RandomObject(1, 2), RandomObject(3, 4)];
final List<RandomObject> copy = original.map((v) => RandomObject.clone(v)).toList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With