skip to Main Content

Its been long time i am suffering between byVal and byRef in dart…

To make myself more clear i have created this demo to explain what i am suffering

I want simply copy of a list and than want to make changes in new list but keeping in mind that original list should be unchanged…

in following program i need to get output like


users =['A','B','C','D']
friends=['AA','BB'];
templist=['AAA','BBB']


but i am getting 


users    =['AAA','BBB','C','D']
friends  =['AAA','BBB'];
templist =['AAA','BBB']


void main() {
  List<User> users = [
    User(name: 'A', age: '43'),
    User(name: 'B', age: '12'),
    User(name: 'C', age: '53'),
    User(name: 'D', age: '22'),
  ];

  List<User> friends = [];
  List<User> templist = [];

  final u1 = users[0];
  friends.add(u1);
  friends[0].name = 'AA';
  final p2 = users[1];
  friends.add(p2);
  friends[1].name = "BB";

  templist = List.from(friends);

  templist[0].name = "AAA";
  templist[1].name = "BBB";

  for (var u in users) {
    print(u.name);
  }

  for (var f in friends) {
    print(f.name);
  }

  for (var t in templist) {
    print(t.name);
  }
}

2

Answers


  1. You need to make copies of the users when filling the other lists.
    Assuming that your User class is something like this

    class User {
      String? name;
      String? age;
    
      User({this.name, this.age});
    
    }
    

    you could add a copy function to it like

    class User {
      String? name;
      String? age;
    
      User({this.name, this.age});
    
      User copy() => User(name: name, age: age);
    }
    

    And then your final code could be something like this

    void main() {
      List<User> users = [
        User(name: 'A', age: '43'),
        User(name: 'B', age: '12'),
        User(name: 'C', age: '53'),
        User(name: 'D', age: '22'),
      ];
    
      List<User> friends = [];
      List<User> templist = [];
    
      final u1 = users[0].copy();
      friends.add(u1);
      friends[0].name = 'AA';
      final p2 = users[1].copy();
      friends.add(p2);
      friends[1].name = "BB";
    
      templist = friends.map((e) => e.copy()).toList();
    
      templist[0].name = "AAA";
      templist[1].name = "BBB";
    
      for (var u in users) {
        print(u.name);
      }
    
      for (var f in friends) {
        print(f.name);
      }
    
      for (var t in templist) {
        print(t.name);
      }
    }
    

    By the way. It’s very common that your data classes are more complicated but have some kind of toJson and fromJson functions. In that case an easy way to make a copy is to call them back to back. So like

    User copy() => User.fromJson(toJson());
    
    Login or Signup to reply.
  2. in your code…

    u are not creating copy but making reference to a list…

    like templist=friends

    that statement do not mean that you are creating new duplicate list but both templist and friends are refering to same list…

    for single object..

    User x=users[0];
    User y=x;
    

    here both x and y refering to same object

    so to make copy

    User y=x.makeCopy();
    

    and for a list of user
    to make copy u need to code as below

    for(var x in friends)
      templist.add(x.makeCopy());
    
    
    void main() {
      List<User> users = [
        User(name: 'A', age: '43'),
        User(name: 'B', age: '12'),
        User(name: 'C', age: '53'),
        User(name: 'D', age: '22'),
      ];
    
      List<User> friends = [];
      List<User> templist = [];
    
      User u1 = users[0];
      friends.add(u1.makeCopy());
      friends[0].name = 'AA';
      User p2 = users[1];
      friends.add(p2.makeCopy());
      friends[1].name = "BB";
    
      for(var x in friends)
        {
          templist.add(x.makeCopy());
        }
    
      templist[0].name = "AAA";
      templist[1].name = "BBB";
    
      for (var u in users) {
        print(u.name);
      }
    
      for (var f in friends) {
        print(f.name);
      }
    
      for (var t in templist) {
        print(t.name);
      }
    }
    

    add method to class

    class User
    {
      String name;
      String age;
      User({required this.name,required this.age});
      User makeCopy() => User(name: name, age: age);
    }
    

    try this code..it will give u same output that u want

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search