You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Is your feature request related to a problem? Please describe.
First off, thanks a lot for creating such an awesome library. I’ve been using Retrofit in my Flutter project, and I handle every backend response by parsing it into a BaseResponse model. However, the body field in this model is generic, which caused me quite a bit of trouble during deserialization. I tried various approaches (Multithreading, CallAdapter etc.) and ended up spending a lot of unnecessary time on this. By chance, I discovered that adding a T body parameter alongside the Map json parameter in the BaseResponse.fromJson method allows Retrofit to handle it automatically. However, since this feature isn’t mentioned in the README, it took me a while to figure it out.
### Describe the solution you'd like
I’d love to see an example or explanation in the README showing how to work with a generic model like BaseResponse with T type body. This would make it much easier for others facing similar situations to find the right solution quickly.
### Describe alternatives you’ve considered
As an alternative, I tried manually deserializing the generic body field using custom solutions and even other deserialization libraries. However, they were either too complex or didn’t fit my project. Learning that Retrofit already supports this functionality was a game-changer and a much cleaner solution.
### Additional context
Here’s a simple example of what I’m referring to:
class BaseResponse<T> {
final int status;
final String description;
final T body;
factory BaseResponse.fromJson(Map<String, dynamic> json, T Function(Map<String, dynamic>) bodyParser) {
return BaseResponse(
status: json['status'],
description: json['description'],
body: bodyParser(json['body']),
);
}
}
@JsonSerializable()
class Group {
int id;
String name;
Group({
required this.id,
required this.name,
});
factory Group.fromJson(Map<String, dynamic> json) => _$GroupFromJson(json);
Map<String, dynamic> toJson() => _$GroupToJson(this);
}
RestApi class:
@RestApi(baseUrl: AppConstant.pointUrl)
abstract class GroupRestApi {
factory GroupRestApi(Dio dio, {String baseUrl}) = _PointContractsRestApi;
@GET('/group/findById')
Future<HttpResponse<BaseResponse<Group>>> getGroupDetail(@Header('Authorization') String authorization, @Query('id') int id);
}
So it's generates like this:
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
late BaseResponse<Group> _value;
try {
_value = BaseResponse<Group>.fromJson(
_result.data!,
(json) => Group.fromJson(json as Map<String, dynamic>),
);
} on Object catch (e, s) {
errorLogger?.logError(e, s, _options);
rethrow;
}
The text was updated successfully, but these errors were encountered:
### Is your feature request related to a problem? Please describe.
First off, thanks a lot for creating such an awesome library. I’ve been using Retrofit in my Flutter project, and I handle every backend response by parsing it into a BaseResponse model. However, the body field in this model is generic, which caused me quite a bit of trouble during deserialization. I tried various approaches (Multithreading, CallAdapter etc.) and ended up spending a lot of unnecessary time on this. By chance, I discovered that adding a T body parameter alongside the Map json parameter in the BaseResponse.fromJson method allows Retrofit to handle it automatically. However, since this feature isn’t mentioned in the README, it took me a while to figure it out.
### Describe the solution you'd like
I’d love to see an example or explanation in the README showing how to work with a generic model like BaseResponse with T type body. This would make it much easier for others facing similar situations to find the right solution quickly.
### Describe alternatives you’ve considered
As an alternative, I tried manually deserializing the generic body field using custom solutions and even other deserialization libraries. However, they were either too complex or didn’t fit my project. Learning that Retrofit already supports this functionality was a game-changer and a much cleaner solution.
### Additional context
Here’s a simple example of what I’m referring to:
RestApi class:
So it's generates like this:
The text was updated successfully, but these errors were encountered: