skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search