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!
Solution 1:
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);
Solution 2:
No as far as open issues seems to suggest:
http://code.google.com/p/dart/issues/detail?id=3367
And specifically:
.. Objects have identity, and you can only pass around references to them. There is no implicit copying.
Solution 3:
Late to the party, but I recently faced this problem and had to do something along the lines of :-
class RandomObject {
RandomObject(this.x, this.y);
RandomObject.clone(RandomObject randomObject): this(randomObject.x, randomObject.y);
int x;
int y;
}
Then, you can just call copy with the original, like so:
final RandomObject original = RandomObject(1, 2);
final RandomObject copy = RandomObject.clone(original);