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

Unhandled Exception: NoSuchMethodError: Class 'int' has no instance method 'toJson' #669

Open savan15 opened 5 months ago

savan15 commented 5 months ago

I have used this package with latest version of this package.

Steps to reproduce the behavior:

1) i have taken your example which is given in your page with some modify here is my code of that file.

@RestApi(baseUrl: 'https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/') abstract class RestClient { factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

@GET('https://httpbin.org/get') Future namedExample( @Query('apikey') apiKey); }

2) after that i have running below command in my terminal as per example given in your page.

dart

dart pub run build_runner build

flutter

flutter pub run build_runner build

3) after running above command i am getting this file which name is "RestClient.g.dart" here i have attach that file for your reference. Note : I have just add code in .txt file becuase .dart extension not supported in github to upload file.

RestClient_g.txt

4) Now when i have running this code in my project i have faced this error in my console.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: Class 'int' has no instance method 'toJson'. E/flutter (20220): Receiver: 3594032520134656 E/flutter (20220): Tried calling: toJson() E/flutter (20220): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5) _RestClient.apiGetMyProfile (package:flutter_asdf/rest_client.g.dart:91:45)

Screenshot 2024-04-24 112501

and this error in this line

5) i have checked more in your example as i have not mentioned data type in query param. that's why this .toJson() method add in RestClient_g.dart file generate with above .toJson() method. now when i have given any type like String, Int then it will work.

@GET('https://httpbin.org/get') Future namedExample( @Query('apikey') apiKey);

My Question is that what if i want to make dynamic suppose i do not want to specify any data type then how to resolve this error.

Expected behavior Not above error come.