class MyModal {
int myField1;
String myField2;
List<MyModal> adjacentNodes;
MyModal(this.myField1,this.myField2);
MyModal.clone(MyModal source) :
this.myField1 = source.myField1,
this.myField2 = source.myField2,
this.adjacentNodes = source.adjacentNodes.map((item) => new MyModal.clone(item)).toList();
}
var secondList = originalList.map((item) => new MyModal.clone(item)).toList();
If a member of MyModal
is of a non-primitive type like String
, int
, double
, num
, bool
, then the clone()
method needs to clone the instances references point to as well.
I think for your use case using immutable values is a better approach, for example with https://pub.dartlang.org/packages/built_value
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…