trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.08k stars 251 forks source link

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

Open vannt1991 opened 2 years ago

vannt1991 commented 2 years ago

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

stale[bot] commented 2 years ago

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.

vannt1991 commented 2 years ago

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

MirzaUkas commented 2 years ago

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