Dart无法将JSON从字符串解析为int

时间:2019-10-31 03:36:05

标签: flutter dart

我正在尝试将JSON值从字符串解析为整数,但被卡住了:( 下面的代码显示了一个HTTP get请求和一个JSON对象,我想要在其中获取Integer中的“ reps”值。

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

调试控制台在行上显示以下错误

  

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'

1 个答案:

答案 0 :(得分:1)

正如异常所解释的,值res ['reps']已经是一个整数,您不需要解析它。

 int repStr = res['reps'];
相关问题