Flutter '_类型错误';不是类型为';字符串';误差颤振图
我想从回应中得到一张地图 我的目标是Flutter '_类型错误';不是类型为';字符串';误差颤振图,flutter,dart,Flutter,Dart,我想从回应中得到一张地图 我的目标是 class Product{ String id; String title; double price; String image; bool sc; Product({@required this.id, @required this.title, @required this.price, @required this.image, @requir
class Product{
String id;
String title;
double price;
String image;
bool sc;
Product({@required this.id, @required this.title, @required this.price,
@required this.image, @required this.sc});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'] as String,
title: json['title'] as String,
price: json['price'] as double,
image: json['image'] as String,
sc: json['sc'] as bool,
);
}
}
类产品{
字符串id;
字符串标题;
双倍价格;
字符串图像;
布尔理工学院;
产品({@required this.id、@required this.title、@required this.price、,
@需要this.image,@required this.sc});
factory Product.fromJson(映射json){
退货(
id:json['id']作为字符串,
title:json['title']作为字符串,
price:json['price']为双精度,
image:json['image']作为字符串,
sc:json['sc']作为bool,
);
}
}
Http代码在这里
Future<Map<String, List<Product>>> fetchProduct(String url) async{
dio.Dio d = new dio.Dio();
Map<String, dynamic> headers = new Map();
headers['Cookie'] = "JSESSIONID=" + SessionUtils().getSession().toString();
dio.Options options = new dio.Options(
headers: headers,
contentType: 'application/json'
);
dio.Response response = await d.get(url, options: options);
print(response.data);
if(response.statusCode == HttpStatus.ok){
return json.decode(response.data); // need help here
}
}
Future-fetchProduct(字符串url)异步{
dio.dio d=新的dio.dio();
映射头=新映射();
headers['Cookie']=“JSSessionId=“+SessionUtils().getSession().toString();
dio.Options Options=新的dio.Options(
标题:标题,
contentType:'应用程序/json'
);
响应=等待d.get(url,选项:选项);
打印(响应数据);
if(response.statusCode==HttpStatus.ok){
返回json.decode(response.data);//此处需要帮助
}
}
我得到了错误I/flatter(20085):引发了另一个异常:类型'\u TypeError'不是类型'String'的子类型。
谢谢
在他们的API参考中,他们提到您必须在选项中设置responseType。默认值是JSON,因此您可以直接返回response.data,因为它已经反序列化到映射中
另一种选择可能是:
responseType = responseType.string;
在dio选项中,然后您将获得字符串形式的response.data,您可以手动解码
我从未使用过DIO,但根据API引用,这似乎是个问题,因为错误表明json.decode()需要字符串,但得到了一个映射,因此response.data可能是动态的,并且数据类型会根据指定的responseType更改。试试看 您能找到
Product
类中的哪一行创建此错误吗?错误可能来自id、标题或图像