{

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


A clone is a copy of an object.

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 the memory location
  • Object(original) has a reference to memory location

In general, Clone is of two types.

  • Shallow Clone: In this copy process, data exists in the same location of 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 objects) and two memory locations exist. each object points 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 example

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 the copied object.

Since the assignment operator allows you to point to the same location with two object references. Here is an example of 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 cause to apply original list.

Here is an example of a 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 clone 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 assigns 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: The 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 the 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, Learn 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

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example