gpt4 book ai didi

json - 从 JSON 映射模型在映射 int 值时给我一个错误

转载 作者:IT王子 更新时间:2023-10-29 06:44:01 26 4
gpt4 key购买 nike

您好,我正在尝试从 JSOn 获取值,但是当我映射一个 INT 值时,flutter 显示该错误并且没有返回数据:

flutter: type 'String' is not a subtype of type 'int'

这是我的模型:

import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';


class Kits {

final String country;
final String franchise;
final String type;
String brand;
String procedure;
String setdesc;
int traymaterialid;
String traydesc;
int referencia;
String descart;
int cantidad;
int homologado;
int bajopresu;
DateTime insdate;


Kits(this.country,this.franchise,this.type, this.brand, this.procedure,this.traydesc, this.traymaterialid);

factory Kits.fromJson(Map<String, dynamic> json) {
return Kits(json['COUNTRY'], json['FRANCHISE'], json['TYPE'], json['BRAND'], json['PROCEDURE'], json['TRAYDESCRIPTION'], json['TRAYMATERIALID'] /* <-- INT*/ );
}

}

class KitsData {

static const url = 'http://app-smith.com/en/kits_API.php?email=miquel@winfor.es&macid=888&passwd=Wcz95f4UGkax5G';
final JsonDecoder _decoder = new JsonDecoder();

Future<List<Kits>> fetch(){
return http.get(url).then((http.Response response){
final String jsonBody = response.body;
//final statusCode = response.statusCode;

/*if (statusCode < 200 || statusCode >= 300 || jsonBody == null){
return List();
}*/

final kitsContainer = _decoder.convert(jsonBody);
final List kitsItems = kitsContainer['kits'];

return kitsItems.map( (kitsRaw) => new Kits.fromJson(kitsRaw) ).toList();

});

}

}

class FetchDataException implements Exception {
String _message;

FetchDataException(this._message);

String toString() {
return "Exception: $_message";
}
}

我不明白为什么会这样,因为 fromJson 只获取我传递的值,然后在我的列表中我将值转换为字符串。

JSON 片段:

{"kits": [{"COUNTRY":"FR","FRANCHISE":"KNEE","TYPE":"LEGION","BRAND":"REVISION","PROCEDURE":"LEGION REVISION","SETDESCRIPTION":"LEGION REVISION - INSTRUMENTS","TRAYMATERIALID":"551820141LRC","TRAYDESCRIPTION":"INSTR LEGION REVISION","REFERENCIA":"71431722","DESCRIPCIONARTICULO":"LGN SCW LWDG TRL S3 5D X 10P","CANTIDAD":"1","HOMOLOGADO":"","BAJOPRESUPUESTO":"","INS_DATE":"2018-08-23 18:57:04"}

最佳答案

所有数值都在引号内,因此它们不是作为 int 而是作为 String 传输的。错误信息是正确的。如果您希望它们作为 String 要么确保值不被引用,如果您不控制使用 int.parse() 将它们从 String< 转换int

关于json - 从 JSON 映射模型在映射 int 值时给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com