0

I am trying to parse a JSON value from string to int but got stuck :( The code below shows a HTTP get request and retrieving a JSON object in which I want to obtain the 'reps' value in Integer.

var response = await httpClient.get(url, headers: {
          'Content-type': 'application/json',
          'Accept': 'application/json',
          'X-API-Key': apikey
        });
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');
        var res = json.decode(response.body);
        String repStr = res['reps'];
        print(repStr);
        int repInt = int.parse(repStr);

The debug console shows the following error on the line

String repStr = res['reps'];

E/flutter ( 8562): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'int' is not a subtype of type 'String'
2
  • 1
    res['reps'] is int already. change to this: int repStr = res['reps']; Commented Oct 31, 2019 at 3:44
  • which type of value you send from server then when you decode json, you will have that type, not every thing is String Commented Oct 31, 2019 at 3:46

1 Answer 1

1

As the exception explains, the value res['reps'] is already an integer you don't need to parse it.

 int repStr = res['reps'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.