is there a way to automatically convert chopper response body to dart model object?

286 views Asked by At

I have been manually converting chopper api response response.body and was wondering if its possible to automatically make a way to convert whatever type of response to whatever dart model object I have.

Below are ways I want to do the conversion of an endpoint's response to a Dart model I have LoginResponse

@ChopperApi()
abstract class ApiService extends ChopperService {
  //
  @Post(path: '/login')
  Future<Response<LoginResponse>> login(
    @Body() body,
  );
 }

static ApiService create() {
final client = ChopperClient(
  baseUrl: Environment.apiUrl,
  converter: const JsonConverter(),
  services: [
    _$ApiService(),
  ],
);
return _$ApiService(client);

}

Here is my LoginResponse model

@JsonSerializable()
class LoginResponse {
  String token;
  User user;

  LoginResponse({
    required this.token,
    required this.user,
  });

  factory LoginResponse.fromJson(Map<String, dynamic> json) =>
      _$LoginResponseFromJson(json);
  Map<String, dynamic> toJson() => _$LoginResponseToJson(this);
}

If I try this way, I get an error flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'LoginResponse?'

0

There are 0 answers