The spread operator will work on Maps e.g. Map<String, dynamic>
so what worked for me was to convert the object to json which is Map<String, dynamic>
, use the spread operator and then convert back to the object.
You can try something like this:
class Something { final String id; final String name; Something({required this.id, required this.name}); factory Something.fromJson(Map<String, dynamic?> json){ return Something( id: json['id'], name: json['name'] ); } Map<String, dynamic?> toJson() { return {'id': id,'name': name }; }}final x = Something(id: 'abc', name: 'def');final y = Something.fromJson({...x.toJson(), 'name': 'somethingElse'})
This might be very expensive to do, but if all you care about is using the spread operator, this should do the job.