我试图把一个响应(从api)在地图上,但我不能使它工作。
下面是代码:Map<String, dynamic> map = jsonDecode(response);
"response"文字显示红色错误,我无法运行应用程序。
The argument type 'Response<dynamic>' can't be assigned to the parameter type 'String'.dartargument_type_not_assignable
我也试过把response.toString()和应用程序甚至运行,但我得到几个错误,如
E/flutter (10331): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter (10331): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:934:48)
E/flutter (10331): #2 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
有人能帮帮我吗
响应json为:
[
{
"Code": 2025,
"Qty": 4,
"Desc": "SERVIÇO DE ALINHAR EIXOS",
"OsNumber": "3862"
},
{
"Code": 204457,
"Qty": 1,
"Desc": "SERVIÇO REPARO",
"OsNumber": "3862"
}
]
您需要使用(response.body)来调用该方法而不是仅仅(响应)。所以你的代码应该是:
var map = jsonDecode(response.body);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
然后
final List<product> loadedProducts = [];
extractedData.forEach((key, value) {
loadedProducts.add(product(
id: key,
title: value['Code'],
description: value['Qty'],
price: value['Desc'],
imageUrl: value['OsNumber'],
));
product是你的
的Model类{
"Code": 2025,
"Qty": 4,
"Desc": "SERVIÇO DE ALINHAR EIXOS",
"OsNumber": "3862"
},
this Object。你会得到对象列表。因此,为这个对象创建一个Model类,并命名为product。飞镖。然后代码就可以工作了
这是你的对象的模型
class product {
int code;
int qty;
String desc;
String osNumber;
product({this.code, this.qty, this.desc, this.osNumber});
product.fromJson(Map<String, dynamic> json) {
code = json['Code'];
qty = json['Qty'];
desc = json['Desc'];
osNumber = json['OsNumber'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Code'] = this.code;
data['Qty'] = this.qty;
data['Desc'] = this.desc;
data['OsNumber'] = this.osNumber;
return data;
}
}