{

Dart| Flutter How to: Multiple ways to clone or copy a List with examples


A clone is a copy of an object. In general, Clone is of two types.

Let’s create a list in dart.

  List original = [1, 2, 3];

It does the following things during list object creation.

  • Create a variable for the list i.e. object

  • list data is stored in memory location

  • Object(original) has a reference to memory location

  • Shallow Clone: In this copy process, data exists in the same memory location, but creates two references pointing to the same memory location. if you add or update the original list, Changes are available in cloned objects.

  • Deep Clone: In the cloning process, two objects(original and cloned object) and two memory locations exist. each object pointe to a different memory location.

if you add or update the original list, Changes do not affect in cloned object.

How to create a List clone in dart and flutter?

There are multiple ways we can do shallow and deep cloning.

Shallow copy list

There are two ways we can copy the references.

  • Assign an object: In this example, the Original list is assigned(=) with a new variable. Here, Modifying the original list does change in the copied object.

Since the assignment operator allows you to point to the same location with two object references. Here is an example for Shallow cloning using the Assign operator.

void main() {
  List original = [1, 2, 3];
  var cloned = original;
  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 3, 5]
}
  • use List casteFrom method:

List casteFrom also creates a copy of references and changes to an original list causes to apply original list.

Here is an example for Shallow copy using List casteFrom.

void main() {
  List original = [1, 2, 3];
  var cloned = List.castFrom(original);

  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 3, 5]
}

Deep copy list

There are multiple ways to create an exact copy of references as well as memory locations. Here, Changes to the original list do not get changes in the copied object.

  • using spread operator(…):

spread operator introduced in dart 2.3 version. spread operator spread data and assign to a new variable by spreading the data.

It is an example of deep cloning using a spread operator.

void main() {
  List original = [1, 2, 3];
  List cloned = [...original];
  original.add(5);
  print(original); //[1, 2, 3, 5]
  print(cloned);//[1, 2, 3]
}
  • use List addAll method

In this example, Appended the original list to clone objects using the addAll() method with an empty array.

The original list is added and removed using add and remove() method. however, the cloned object does not remove or added with new elements.

It is an example of deep cloning using the List addAll method.

void main() {
  List original = [1, 2, 3];
  List cloned = []..addAll(original);
  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}

  • use List from method:

List provides a named constructor using the from() method which accepts a List of dynamic types.

It is an example of a deep copy using the List from the method.

void main() {
  List original = [1, 2, 3];
  List cloned = List.from(original);
  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}
  • List unmodifiable method: lists are copied using immutable(List unmodifiable method) It is an example of a deep copy using the List unmodifiable method.
void main() {
  List original = [1, 2, 3];
  var cloned = List.unmodifiable(original);
  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}
  • List unmodifiable method: List of the method provides another way to create a copy of the list from the existing list.

It is an example of a deep copy using the List of the method.

void main() {
  List original = [1, 2, 3];
  var cloned = List.of(original);

  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}
  • use List toList method: The list toList method creates a deep copy of an original list.

It is an example of a deep copy using the List toList method.

void main() {
  List original = [1, 2, 3];
  var cloned = original.toList();

  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}
  • use List generate method: Another way is to create a growable list using the list generate method, It creates two objects and references. example code for a deep copy using List generates method.
void main() {
  List original = [1, 2, 3];
  var cloned = List.generate(original.length, (index) => original[index]);

  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}
  • use List map method: Finally, iterate an original list using the stream map method, convert the stream of elements into a list using the toList() method

example code for a deep copy using the List map method.

void main() {
  List original = [1, 2, 3];
  List cloned = original.map((item)=>item).toList();

  original.add(5);
  original.remove(2);

  print(original); //[1, 3, 5]
  print(cloned); //[1, 2, 3]
}

Conclusion

To summarize, Learned multiple ways to clone or copy an object with a shallow and deep clone with examples

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example