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

前端 未结 11 2288
谎友^
谎友^ 2020-12-01 13:22

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?<

11条回答
  •  無奈伤痛
    2020-12-01 14:23

    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);
    

提交回复
热议问题