Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
savan15 opened this issue Apr 24, 2024 · 0 comments

Comments

@savan15
Copy link

savan15 commented Apr 24, 2024

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);
}

  1. 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

  1. 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

  1. 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

  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant