我不知道如何解决此错误。
发生异常。 _TypeError(类型'(dynamic)=> Null'不是'f'类型'(String,dynamic)=> void'的子类型)
class _ShowformState extends State<Showform> {
List<MaintenanceInfo> info = [];
@override
void initState() {
super.initState();
fetchData();
}
void fetchData() async {
final data = await http.get('http://192.168.1.35:7000/');
print(data.body);
final jsonData = json.decode(data.body);
setState(() {
jsonData.forEach((v) {
var tmpData = MaintenanceInfo.fromJson(v);
info.add(tmpData);
});
});
}
MaintenanceInfo maintenanceInfoFromJson(String str) =>
MaintenanceInfo.fromJson(json.decode(str));
String maintenanceInfoToJson(MaintenanceInfo data) =>
json.encode(data.toJson());
class MaintenanceInfo {
MaintenanceInfo(
{this.serial,
this.model,
});
String serial;
String model;
factory MaintenanceInfo.fromJson(Map<String, dynamic> json) =>
MaintenanceInfo(
serial: json['serial'],
model: json['model'],);
Map<String, dynamic> toJson() => {
'serial': serial,
'model': model, };}
JSON格式如下
{ "data": [
{
"serial": "8850124003850",
"model": "280",
},]
}
Ps。我是扑扑开发的新手。
答案 0 :(得分:0)
您的模型看起来有问题。您需要像这样修改它:
class MaintenanceInfo {
List<Data> data;
MaintenanceInfo({this.data});
MaintenanceInfo.fromJson(Map<String, dynamic> json) {
if (json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String serial;
String model;
Data({this.serial, this.model});
Data.fromJson(Map<String, dynamic> json) {
serial = json['serial'];
model = json['model'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['serial'] = this.serial;
data['model'] = this.model;
return data;
}
}
您可以使用此website将JSON转换为Dart模型。