Dart/Flutter – How to clone/copy a list

In this post, we will learn how to clone/copy an original list to new list. But before that, let’s look at below example to understand why we need to clone/copy, instead of using original list.

void testAssignList() {
  print('Test assign list');
  var numbers = [1, 2, 3];
  var newNumbers = numbers;
  // Print the first element of new list
  print(newNumbers[0]);

  // Change original list 
  numbers[0] = 100;
  // Try to print the first element of new list again
  print(newNumbers[0]);
}
Test assign list
1
100


As you see, when we change the value of original list (numbers – line 9), the value of new list (newNumbers) is updated too. The reason is when we assign (using “=”), new list (newNumbers) will “refer” or “point” to the same memory of original list (numbers). In other words, number and newNumbers are just 2 names of the same list. Changing on either list will affect the other one.

The same problem happens if we use other kind of assign operator, like the static method castFrom()

var newNumbers = List.castFrom(numbers);


๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

In many cases, we will need to keep the original list and work on the cloned version of it. That is when we have to clone/copy, instead of assign. Dart provides some factory constructors to satisfy this requirement:

var newNumbers = List.from(numbers);
var newNumbers = List.generate(numbers.length, (index) => numbers[index]);
var newNumbers = List.of(numbers);
var newNumbers = List.unmodifiable(numbers);

All the code

void main() {
  testAssignList();

  testCreateListUsingCastFromMethod();

  print('* * * * * * * * * * ');

  testCreateListUsingFromMethod();

  testCreateListUsingGenerateMethod();

  testCreateListUsingOfMethod();

  testCreateListUsingUnmodifiableMethod();
}

void testAssignList() {
  print('Test assign list');
  var numbers = [1, 2, 3];
  var newNumbers = numbers;
  // Print value of new list
  print(newNumbers[0]);

  // Change original list
  numbers[0] = 100;
  // Try to print the value of new list again
  print(newNumbers[0]);
}

void testCreateListUsingCastFromMethod() {
  print('Create list using List.castFrom() method');
  var numbers = [1, 2, 3];
  var newNumbers = List.castFrom(numbers);
  print(newNumbers[0]);

  numbers[0] = 100;
  print(newNumbers[0]);
}

void testCreateListUsingFromMethod() {
  print('Create list using List.from() method');
  var numbers = [1, 2, 3];
  var newNumbers = List.from(numbers);
  print(newNumbers[0]);

  numbers[0] = 100;
  print(newNumbers[0]);
}

void testCreateListUsingGenerateMethod() {
  print('Create list using List.generate() method');
  var numbers = [1, 2, 3];
  var newNumbers = List.generate(numbers.length, (index) => numbers[index]);
  print(newNumbers[0]);

  numbers[0] = 100;
  print(newNumbers[0]);
}

void testCreateListUsingOfMethod() {
  print('Create list using List.of() method');
  var numbers = [1, 2, 3];
  var newNumbers = List.of(numbers);
  print(newNumbers[0]);

  numbers[0] = 100;
  print(newNumbers[0]);
}

void testCreateListUsingUnmodifiableMethod() {
  print('Create list using List.unmodifiable() method');
  var numbers = [1, 2, 3];
  var newNumbers = List.unmodifiable(numbers);
  print(newNumbers[0]);

  numbers[0] = 100;
  print(newNumbers[0]);
}
Test assign list
1
100
Create list using List.castFrom() method
1
100
* * * * * * * * * * 
Create list using List.from() method
1
1
Create list using List.generate() method
1
1
Create list using List.of() method
1
1
Create list using List.unmodifiable() method
1
1

Hope this post is useful for you!ย 
If you are looking for a developer or a team to build you next projects, pleaseย Drop us your request!

Tagged : / / / / /
5 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x