Open Jaew00Shin opened 1 year ago
The problem here might be that dart extensions are not really extending the type and Retrofit generator is not aware of your extensions.
My "workaround" for this is to use custom types called ApiDateTime
and optionally ApiDate
if it's a more "pretentious" API:
class ApiDateTime {
static final DateFormat dateFormat = DateFormat('<your API accepted date-time format>');
final DateTime dateTime;
ApiDateTime(this.dateTime);
factory ApiDateTime.fromJson(String json) {
return ApiDateTime(dateFormat.parse(json));
}
String toJson() {
return dateFormat.format(dateTime);
}
}
Let's say we have a legacy API that accepts a different format for properties that should only contain the date part. I can have an additional type with a different DateFormat
:
class ApiDate {
static final DateFormat dateFormat = DateFormat('<your API accepted date format>');
final DateTime dateTime;
ApiDate(this.dateTime);
factory ApiDate.fromJson(String json) {
return ApiDate(dateFormat.parse(json));
}
String toJson() {
return dateFormat.format(dateTime);
}
}
I put the workaround in quotes because I actually found it as a very good practice as it gives me full control over how dates are serialized without resorting to interceptors or other more advanced methods.
When I write this code, there is an error like below.
I want to use various class type for @Part() parameter. Although I defined toJson extension, it does not work. it is failed to generate code.
Please fix it.