Flutter API数据解析问题:无法获取Items类中的Address列表
Flutter Fetch API 地址列表解析问题
我正在练习Flutter的Fetch API,目前能获取部分数据,但Items类里的Address列表信息无法正常解析,不确定当前实现是否正确,求帮忙排查。
获取数据的代码
List<Items> _list = []; List<Items> _search = []; var loading = false; Future fetchMos() async { setState(() { loading = true; }); _list.clear(); var client = http.Client(); String mosUrl = ''; var url = Uri.parse(mosUrl); var headers = {'Client-Token': ''}; var response = await client.get(url, headers: headers); if (response.statusCode == 200) { var data = jsonDecode((utf8.decode(response.bodyBytes)))['items']; setState(() { for (Map i in data) { _list.add(Items.fromJson(i)); loading = false; } }); } }
数据模型类代码
class Items { String? custnum; String? name; List<Address>? address; Items({this.custnum, this.name, this.address}); Items.fromJson(json) { custnum = json['custnum']; name = json['name']; if (json['address'] != null) { address = <Address>[]; json['address'].forEach((v) { address!.add(new Address.fromJson(v)); }); } } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['custnum'] = this.custnum; data['name'] = this.name; if (this.address != null) { data['address'] = this.address!.map((v) => v.toJson()).toList(); } return data; } } class Address { int? shipto; String? addr1; String? thanon; String? tambon; String? amphur; String? provCode; String? province; String? country; String? phone; String? email; String? postcode; String? contact; String? latitude; String? longitude; String? fax; String? soi; String? zipcode; Address({ this.shipto, this.addr1, this.thanon, this.tambon, this.amphur, this.provCode, this.province, this.zipcode, this.country, this.phone, this.email, this.postcode, this.contact, this.latitude, this.longitude, this.fax, this.soi }); Address.fromJson(json) { shipto = json['shipto']; addr1 = json['addr1']; thanon = json['thanon']; tambon = json['tambon']; amphur = json['amphur']; provCode = json['prov_code']; province = json['province']; zipcode = json['zipcode']; country = json['country']; phone = json['phone']; email = json['email']; postcode = json['postcode']; contact = json['contact']; latitude = json['latitude']; longitude = json['longitude']; fax = json['fax']; soi = json['soi']; } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['shipto'] = this.shipto; data['addr1'] = this.addr1; data['thanon'] = this.thanon; data['tambon'] = this.tambon; data['amphur'] = this.amphur; data['prov_code'] = this.provCode; data['province'] = this.province; data['zipcode'] = this.zipcode; data['phone'] = this.phone; data['email'] = this.email; data['postcode'] = this.postcode; data['contact'] = this.contact; data['longitude'] = this.longitude; data['fax'] = this.fax; data['soi'] = this.soi; return data; } }
排查关键点
- 模型字段遗漏:Address类的
toJson方法缺失latitude字段,会导致数据序列化/反序列化不一致,需添加data['latitude'] = this.latitude;。 - 空安全风险:
Items.fromJson中直接使用address!.add,若address初始化异常会触发空指针,建议用if (address != null)包裹操作。 - loading状态错误:循环内重复设置
loading = false,应将该语句移到循环外,确保所有数据加载完成后再更新状态。 - 类型转换不严谨:
for (Map i in data)中data为动态类型,建议先转为List<Map<String, dynamic>>避免类型错误:List<Map<String, dynamic>> itemsList = List.castFrom(data); for (var i in itemsList) { _list.add(Items.fromJson(i)); } - API字段匹配:确认API返回的
address是数组类型,且内部字段名与模型完全匹配(比如prov_code对应provCode是否正确)。
内容的提问来源于stack exchange,提问作者Ppnncs
相关产品推荐
相关产品推荐

