I am using Freezed for serialization. I need to get the colour from the database which is String like this "#FFFFFF". I need when I get this string, converting it to int to as a colour. but I get the error:
I/flutter ( 4483): ** [Error] type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast. isError: [true]
The code is:
@freezed
class Categories with _$Categories {
factory Categories({
required int id,
@ColorResponseConverter() required int colour,
}) = _Categories;
factory Categories.fromJson(Map<String, dynamic> json) =>
_$CategoriesFromJson(json);
}
the code of the converter is:
class ColorResponseConverter implements JsonConverter<int, Map<String, dynamic>> {
const ColorResponseConverter();
@override
int fromJson(Map<String, dynamic> json) {
if (json['color'] != null) {
return int.parse((json['color'] as String).replaceFirst("#", "0xFF"));
} else {
return 0xFFFFFFFF;
}
}
@override
Map<String, dynamic> toJson(int color) =>
{"color": color.toString().replaceFirst("0xFF", "#")};
}