3

i am making a list in flutter from an existing list and when i used this new list and perform some action on it like change its quantity parameter it changes the parameter of existing list also. Can some one tell me where i am going wrong.

Existing list:

  List<ItemsModel> originalList=[
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
  ];

New list:

  List<ItemsModel> updatedItemList;
      void initState() {
        updatedItemList=[];
        originalList.forEach((val){
      updatedItemList.add(val);
    });
        super.initState();
      }

on pressed button:

 updatedItemList[i].quantity=qtyReceive

And on some button pressed i am performing this action that was updating both lists. Why??

Umair
  • 1,759
  • 6
  • 23
  • 44
  • 1
    Both lists store references to the same `ItemsModel` objects. Mutations to those objects will be reflected in both lists. If you want them to be independent, you'll need to make a *deep* copy, which will require creating new `ItemsModel` instances. – jamesdlin Jun 20 '20 at 05:29
  • How can i make that one in my case? – Umair Jun 20 '20 at 05:34
  • You would have to look at the documentation for whatever your `ItemsModel` class is. – jamesdlin Jun 20 '20 at 05:36
  • as @jamesdlin mentioned we can create the new objects based on the original list items. Added an example below. – dev-aentgs Jun 20 '20 at 05:39

2 Answers2

6

As @jamesdlin mentioned, creating a deep copy will solve the issue by change reference to new ItemsModel objects in the list.

Small example of creating deep copy:

class ItemsModel{
  String name;
  String quantity;
  ItemsModel({this.name,this.quantity});
}


void main() {
   List<ItemsModel> originalList=[
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
    ItemsModel(name: 'High Quality Double Spring Tummy Trimmer For Unisex',quantity: '30'),
  ];

  List<ItemsModel> updatedItemList;
  updatedItemList=[];
  originalList.forEach((val){
      //creating new ItemsModel objects and adding to the updatedList
      updatedItemList.add(ItemsModel(name:val.name,quantity:val.quantity));
    });

  print('Original List');
  for(var listItem in originalList){
    listItem.quantity = '60';
    print('${listItem.name},${listItem.quantity}');
  }

  print('Updated List');
    for(var listItem in updatedItemList){
    print('${listItem.name},${listItem.quantity}');
  }
}
dev-aentgs
  • 1,218
  • 2
  • 9
  • 17
4

I am late to the party, but anyways this can help someone else who come here Solution- Use

myList = List.from(mynewlist); 

instead of

mylist = mynewlist;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • The "from" method does a copy of the List but does not perform a deep copy as the question intended. – Lee KM May 02 '23 at 01:38