Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy list values to another list in flutter

Tags:

list

copy

flutter

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

like image 361
Alvin John Niravukalayil Avatar asked Oct 15 '19 07:10

Alvin John Niravukalayil


People also ask

How do I append a list to a list in flutter?

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.


5 Answers

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

like image 52
Ibrahim Karahan Avatar answered Oct 05 '22 23:10

Ibrahim Karahan


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

like image 31
Logemann Avatar answered Oct 05 '22 23:10

Logemann


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

like image 32
Johan Witters Avatar answered Oct 06 '22 00:10

Johan Witters


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.

like image 28
Atul Singh Avatar answered Oct 05 '22 23:10

Atul Singh


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();
    
like image 35
Paul Avatar answered Oct 06 '22 01:10

Paul