How to copy list values to another list in flutter
Asked Answered
K

6

45

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

Katabatic answered 15/10, 2019 at 7:41 Comment(0)
I
100

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

Immuno answered 15/10, 2019 at 7:54 Comment(6)
mylist = List.from(mynewlist);Bromoform
Its weird but streamDataListExtra = streamDataList; works for me.Esbjerg
@Esbjerg Yes, it might work for you, but you will not actually copy the value of the list. You only copied the pointer from the initial list.Reynolds
This doesn't work with me because my list has sub-list, the main list it is cloned properly, but if i filter the sub-list the original list affected even when i use myList = List.from(mynewlist); any idea?Belle
@OsamaRemlawi for me, its affected just cloned list. jdoodle.com/ia/hVsImmuno
I've got the answer, and i liked to post it here as a reference for deep copy #69514866Belle
S
14

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

Scrimpy answered 15/10, 2019 at 7:51 Comment(0)
S
13

use:

myNewList = [...myOldList]

it creates a shallow copy of items from myOldList to myNewList

for more information: you can search for 'spread operator in dart'.

Street answered 29/11, 2022 at 10:42 Comment(0)
S
9

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

Sapsucker answered 26/1, 2021 at 20:54 Comment(0)
R
5

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();
    
Reynolds answered 30/9, 2021 at 16:16 Comment(1)
Source inspired: deep copy objectReynolds
J
2

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.

Jimmie answered 1/7, 2021 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.