0

I try to modify an object:

void main() {
  
  Model item = Model(list: [3,4]);
  print(item.list);
  Model test = item;
  List modifyList = test.list!;
  modifyList[0] = 999;
  test = Model(list: modifyList);
  print("original: " + item.list.toString());
  print("modified: " + test.list.toString());
  
}
  
  class Model {
  String? title;
  String? postid;
  List? list;

  Model({
    this.title,
    this.postid,
    this.list,
  });
    
    
}

this prints:

[3, 4]
original: [999, 4]
modified: [999, 4]

I dont understand why the first parameter of the original test model is also modified. How can I modify only the test model and keep the original?

Whats the reason for modifying the original variable even though I have previously put it to the new test?

desmeit
  • 550
  • 1
  • 7
  • 24
  • ``Model test = item;`` here you're passing the reference of the model to new model. That's all and since it's a reference, it will modify the original too. – OMi Shah Jun 28 '23 at 10:26
  • thanks. so I have to create a copy of the model? But Model test = Model( list: item.list); is the same thing. How can I create a independent copy? – desmeit Jun 28 '23 at 10:30
  • I think ``Model copy = Model.from(old)`` should work. Please check – OMi Shah Jun 28 '23 at 10:37

2 Answers2

1

You can't do this cause it's references to original object

Try to deep copy change the value

Model item = Model(list: [3,4]);
  print(item.list);
  Model test = item.copyWith(list: [999,item.list![1]]);
  print("original: " + item.list.toString());
  print("modified: " + test.list.toString());
  
}
  
  class Model {
  String? title;
  String? postid;
  List? list;

  Model({
    this.title,
    this.postid,
    this.list,
  });
  /// Add copyWith to mutate an the original object
  Model copyWith({
    String? title,
  String? postid,
  List? list,
  }) {
    return Model(title: title??this.title, postid:postid??this.postid, list:list ??this.list);
  }
}

output:

[3, 4]
original: [3, 4]
modified: [999, 4]
Jason
  • 139
  • 1
  • 2
  • Looks nice. Is there a way to modify the list if I cant create it manually? I have an index and need to change it at this position. – desmeit Jun 28 '23 at 10:55
0

I ended up to adapted the copyWith from @Jason and added List.from to clone the list and remove the reference:

void main() {
  
  Model item = Model(list: [3,4]);
  print(item.list);
  
  List modifyList = List.from(item.list!);
  modifyList[0] = 999;
  
  Model test = item.copyWith(list: modifyList);


  print("original: " + item.list.toString());
  print("modified: " + test.list.toString());
  
}
  
  class Model {
  String? title;
  String? postid;
  List? list;

  Model({
    this.title,
    this.postid,
    this.list,
  });
    
   Model copyWith(
      {String? title,
      String? postid,
      List? list,
      }) {
    return Model(
        title: title ?? this.title,
        
        postid: postid ?? this.postid,
      
        list: list ?? this.list,
       );
  }
    
 
    
    
}

output:

[3, 4]
original: [3, 4]
modified: [999, 4]

clone list without reference: https://stackoverflow.com/a/71031170/3037763

other way is .toList(): https://stackoverflow.com/a/62013491/3037763

desmeit
  • 550
  • 1
  • 7
  • 24