Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clone an Object (deep copy) in Dart?

Is there a Language supported way make a full (deep) copy of an Object in Dart?

Secondary only; are there multiple ways of doing this, and what are the differences?

Thanks for clarification!

like image 996
george koller Avatar asked Oct 28 '12 10:10

george koller


People also ask

Does clone create a deep copy?

clone() is indeed a shallow copy. However, it's designed to throw a CloneNotSupportedException unless your object implements Cloneable . And when you implement Cloneable , you should override clone() to make it do a deep copy, by calling clone() on all fields that are themselves cloneable.

What is object deep clone?

A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made.


1 Answers

Darts built-in collections use a named constructor called "from" to accomplish this. See this post: Clone a List, Map or Set in Dart

Map mapA = {     'foo': 'bar' }; Map mapB = new Map.from(mapA); 
like image 73
computmaxer Avatar answered Oct 15 '22 23:10

computmaxer