21

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

Alvin John Niravukalayil
  • 1,320
  • 1
  • 13
  • 23

5 Answers5

51

Use myList = List.from(mynewlist); instead of mylist = mynewlist;

dKen
  • 3,029
  • 1
  • 25
  • 35
Ibrahim Karahan
  • 2,206
  • 1
  • 6
  • 18
9

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.

enter image description here

Logemann
  • 2,457
  • 29
  • 48
6

var YOURCOPY = YOURLIST.map((v) => v).toList();

dKen
  • 3,029
  • 1
  • 25
  • 35
Johan Witters
  • 1,463
  • 9
  • 21
1

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.

Atul Singh
  • 21
  • 3
1

Deep copy of a custom class List

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:

  1. 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;
    }
    
  2. 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();
    
Paul
  • 588
  • 6
  • 16