Flutter中CompanyBranch.fromJson()返回空值的技术求助
问题排查:JSON映射CompanyBranch对象返回空值
问题描述
使用自研API获取JSON数据,服务器已返回序列化数据,但将响应映射为CompanyBranch对象时返回空值。
代码片段
Map<String, dynamic> resp = jsonDecode(response.body); CompanyBranch companyBranch = CompanyBranch.fromJson(resp); // 返回null LatLng coords = LatLng(companyBranch.lat, companyBranch.lng); return coords;
服务器返回的JSON响应
{ "id": 1, "name": "Fixing Now Surcursal", "outdoorNumber": 56, "internalNumber": 0, "street": "Si", "postalCode": 91193, "district": "No se que poner", "lat": -33.3017267, "lng": -66.3377522, "active": true, "creationDate": "2022-11-10T20:40:46.228762Z", "state": 1, "municipally": 1 }
映射用的CompanyBranch类代码
class CompanyBranch { int id; String name; int outdoorNumber; int? internalNumber; String street; int postalCode; String distric; String? state; String? municipality; double lat; double lng; bool active; DateTime creationDate; CompanyBranch({ required this.id, required this.name, required this.outdoorNumber, this.internalNumber, required this.street, required this.postalCode, required this.distric, required this.lat, required this.lng, required this.active, required this.creationDate, this.state, this.municipality, }); factory CompanyBranch.fromJson(Map<String, dynamic> json) { return CompanyBranch( id: json['id'] as int, name: json['name'] as String, outdoorNumber: json['outdoorNumber'] as int, internalNumber: json['internalNumber'] as int, street: json['street'] as String, postalCode: json['postalCode'] as int, distric: json['district'] as String, lat: json['lat'] as double, lng: json['lng'] as double, active: json['active'] as bool, creationDate: json['creationDate'] as DateTime, state: json['state'] as String, municipality: json['municipaly'] as String, ); } }
错误分析与修复方案
对象返回null的核心原因是fromJson方法中存在类型转换失败和字段拼写错误,导致初始化抛出异常,最终对象创建失败。
1. 字段拼写错误
- JSON中的键是
municipally,但fromJson里写成了municipaly(少了字母l),无法正确取值。 - 类属性名
distric建议改为district,和JSON键名保持一致,避免后续混淆。
2. 类型转换错误
creationDate:JSON返回的是字符串,不能直接用as DateTime,必须通过DateTime.parse()解析。state:JSON返回的是数字类型(1),但类中定义为String?,直接as String会抛出类型转换异常,需转为字符串。internalNumber:类中定义为可选参数(int?),但as int无法处理null值,应改为as int?。lat/lng:JSON中的数值可能被解析为int或double,直接as double会报错,建议用(json['key'] as num).toDouble()兼容两种类型。
修复后的fromJson方法
factory CompanyBranch.fromJson(Map<String, dynamic> json) { return CompanyBranch( id: json['id'] as int, name: json['name'] as String, outdoorNumber: json['outdoorNumber'] as int, internalNumber: json['internalNumber'] as int?, street: json['street'] as String, postalCode: json['postalCode'] as int, distric: json['district'] as String, // 若类属性名改为district,这里也同步修改 lat: (json['lat'] as num).toDouble(), lng: (json['lng'] as num).toDouble(), active: json['active'] as bool, creationDate: DateTime.parse(json['creationDate'] as String), state: json['state'].toString(), municipality: json['municipally'] as String?, ); }
额外建议
- 推荐使用
json_serializable等代码生成库自动生成序列化/反序列化代码,避免手动编写时出现拼写和类型错误。 - 对可选参数添加null判断,比如
json['internalNumber'] != null ? json['internalNumber'] as int : null,增强代码健壮性。
内容的提问来源于stack exchange,提问作者Arturo Montes de Oca Barrios
相关产品推荐
相关产品推荐

