How can I clone an object with dart_mappable without serializing/deserializing?
I would like to simply:
import 'dart:ui';
import 'package:dart_mappable/dart_mappable.dart';
part 'point.mapper.dart';
@MappableClass()
class Point with PointMappable {
final Offset coordinates;
final String type;
Point(this.coordinates, this.type);
}
void main() {
var point1 = Point(Offset.zero, 'this type');
var point2 = point1.copyWith();
assert(point1 != point2);
}
but it fails.
It only works if I actually change some property on the copyWith call like:
var point2 = point1.copyWith(type: 'other type');
but as I want my properties to be final, I can’t do it and have my desired copy of the original. Or I would need to do 2 copyWiths in a row, which seems silly:
var pointTemp = point1.copyWith(type: 'other type');
var point2 = pointTemp.copyWith(type: point1.type);
Is there some way to clone with dart_mappable without serializing/deserializing? Using copyWith would be a plus?
2
Answers
See the correct usage of dart_mappable package. I have added few missing lines. See the comments. Think this would work
In order to generate the serialization code, run the following command:
You’ll need to re-run code generation each time you are making changes to your annotated classes. During development, you can use watch to automatically watch your changes:
dart pub run build_runner watch
And obviously you would have added necessary dev dependencies I believe.
The mixin
PointMappable
generated by thedart_mappable
builder overrides the operator==
and the getterhashCode
so that objects of typePoint
are compared using the class variablescoordinates
andtype
.For this reason I would expect: