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 246 forks source link

Error in retrofit generated file -- constructor Response.fromJson not defined #316

Closed mohammad-melhem closed 3 years ago

mohammad-melhem commented 3 years ago

I'm using retrofit in my flutter application and I'm using json_serialzable as well to generate model classes (I followed the steps in the Readme section). When I run flutter pub run build_runner build --> it successfully generates the part file for both retrofit service (by retrofit generator) and model that contains fromJson and toJson (I'm using json_annotation lib with json_serializable to generate fromJson and toJson as stated in the readme as well in the Task example). The problem is that the class generted by retrofit has an error that Response doesn't have the method fromJson. Here is a snippet from the generated file of a user service

image

and here are the dependencies in my pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  get: ^3.24.0
  flutter_custom_clippers: ^1.1.2
  font_awesome_flutter: ^8.11.0
  firebase_core: ^0.7.0
  firebase_auth: ^0.20.1
  cloud_firestore: ^0.16.0+1
  google_sign_in: ^4.5.9
  chips_choice: ^2.0.1
  logging: ^0.11.4
  path_provider: ^1.6.27
  hive: ^1.4.4+1
  hive_flutter: ^0.3.1
  retrofit: ^1.3.4+1
  dio: ^3.0.10
  json_annotation: ^3.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  hive_generator: ^0.8.2
  retrofit_generator: ^1.3.7+5
  build_runner: ^1.10.0
  json_serializable: ^3.5.0

Flutter 2.0.1 • channel stable • https://github.com/flutter/flutter.git Framework • revision c5a4b4029c (12 days ago) • 2021-03-04 09:47:48 -0800 Engine • revision 40441def69 Tools • Dart 2.12.0

Note: I've also tried to use 'any' instead of a certain version (even though no problem with pub get) but still the same result. Your help is much appreciated and thank you for the awesome library and great efforts in advance.

rinlv commented 3 years ago

Hi @mohammad-melhem, after a few hours of research, I found retrofit_genrator to add a new method isGenericArgumentFactories to check class is generic. So, just add:

@JsonSerializable(genericArgumentFactories: true)

to resolve this error.

mohammad-melhem commented 3 years ago

Hi @rinlv , firstly, thank you very much for your effort and sorry if it took a while till I respond. I tried the suggested solution but it didn't fix the issue and I got the following in the logs:

The class User is annotated with JsonSerializable field genericArgumentFactories: true. genericArgumentFactories: true only affects classes with type parameters. For classes without type parameters, the option is ignored.

and as it is stated my user class don't have any type parameters, here is the user model I'm using:

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable(includeIfNull: false, genericArgumentFactories: true)
class User {
  int id;
  int userType;
  String firstName;
  String lastName;
  String email;
  String profilePicture;
  bool isOpenForRemote;

  User(
      {this.id,
      this.userType,
      this.firstName,
      this.lastName,
      this.email,
      this.profilePicture,
      this.isOpenForRemote});

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}
rinlv commented 3 years ago

Hi @mohammad-melhem,

In this case,

@JsonSerializable(genericArgumentFactories: true)

only work when you added on the Response class, not the User class.

mohammad-melhem commented 3 years ago

Hi @rinlv, yes you are right as Response is the generic class. Thank you very much for your help.

AgarwalsRahul commented 1 year ago

Hi, How did you manage to get response object ?