我找不到解决方案。我在点击http.post时遇到上述异常
这是Api提供程序类==>
class SitesApiProvider {
final _url = ApiStrings.GET_SITES_BY_COMPANY_ID_API;
final _body= {
"company_id" : 10
};
Future<SiteModel> fetchSitesList() async {
print("inside sites list api");
final response = await http.post(_url, body:_body);
print("${response.body.toString()}");
print("json body --- ${json.encode(body)}");
if (response.statusCode == 200) {
return SiteModel.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
}
我缺少什么了吗???
最佳答案
在http包文档中,您可以找到here,post方法的主体如下:
但是您正在尝试传递一个带有String键和int值的Map,这是不可能的,这就是为什么您会收到错误消息。您可以将大包存储为字符串,并在收到时将其转换为int。
final Map<String,String> _body= {
"company_id" : 10.toString() //your value is now String
};
并通过此更改,您的代码必须正常工作。
关于rest - 未处理的异常:类型 'int'不是类型转换中类型 'String'的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60937660/