Closed bounty1342 closed 4 years ago
@k-paxian : Actually it's not related to Equatable, but rather to another corner case of a getter of a list of Object containing a Enum.
enum MyColor { black, red }
@jsonSerializable
class MyCarModel {
final String model;
@JsonProperty(enumValues: MyColor.values)
final MyColor color;
const MyCarModel({this.model, this.color});
List<Object> get myVal => [
model,
color,
];
Map<String, dynamic> toJson() {
return JsonMapper.toMap(this);
}
}
Certainly because getter are consider like field, it's expecting an annotation for myVal. The following works witch seems to confirm the expectation :
enum MyColor { black, red }
@jsonSerializable
class MyCarModel {
final String model;
@JsonProperty(enumValues: MyColor.values)
final MyColor color;
const MyCarModel({this.model, this.color});
@JsonProperty(enumValues: MyColor.values)
Object get props => color;
Map<String, dynamic> toJson() {
return JsonMapper.toMap(this);
}
}
Since this bug is resolve, one easy way to make it compatible with Equatable is to add the 'ignore' @ on the getter containing Enum.
@jsonSerializable
class MyCarModel extends Equatable {
final String model;
@JsonProperty(enumValues: MyColor.values)
final MyColor color;
const MyCarModel({this.model, this.color});
@JsonProperty(ignore: true)
List<Object> get props => [
model,
color,
];
Map<String, dynamic> toJson() {
return JsonMapper.toMap(this);
}
}
Hope this findings will help you and other users. 😃
So nice to see clean and concise model code 😄
Hi,
Run into this issue an issue with equatable today.
The following code :
Will trigger this exception :
Regards