retrofit.dart icon indicating copy to clipboard operation
retrofit.dart copied to clipboard

issue `@JsonSerializable(genericArgumentFactories: true)` combine with `Use Flutter compute for parsing models`

Open vannt1991 opened this issue 2 years ago • 3 comments

hi @FaKenKoala ,

I have faced with a problem when trying to use @JsonSerializable(genericArgumentFactories: true) combine with Use Flutter compute for parsing models, it's generated an error code and I try to manual modified generated code to : 1, in JSON model

BaseResponse<T> deserializeBaseResponse<T>(List params) =>
    BaseResponse.fromJson(params.firstOrNull, params.lastOrNull);
Map<String, dynamic> serializeBaseResponse<T>(List params) =>
    params.firstOrNull?.toJson(params.lastOrNull);

@JsonSerializable(
  genericArgumentFactories: true,
)
class BaseResponse<T> {
  final T? data;
  final List<Message>? messages;
  BaseResponse(this.data, this.messages); 

2, in retrofit generated code for service when using @RestApi(parser: Parser.FlutterCompute,)

final BaseResponse<List<ApplicationResponse>> value =
       await compute(deserializeBaseResponse, [
     _result.data!,
     (json) => (json as List<dynamic>)
         .map<ApplicationResponse>(
             (i) => ApplicationResponse.fromJson(i as Map<String, dynamic>))
         .toList()
   ]);
   final httpResponse = HttpResponse(value, _result);
   return httpResponse;

and it's working fine, but for the long term, could u add more code logic to support this issue.

Thanks for your effort!

Originally posted by @vannt1991 in https://github.com/trevorwang/retrofit.dart/issues/399#issuecomment-1070439099

vannt1991 avatar Mar 18 '22 03:03 vannt1991

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 17 '22 13:04 stale[bot]

@FaKenKoala could u take a look at it, many thanks!

vannt1991 avatar Apr 17 '22 15:04 vannt1991

It's great if this feature can be provided. multi-argument is needed for generic classes

MirzaUkas avatar Jun 18 '22 15:06 MirzaUkas