[英]type 'bool' is not a subtype of type 'List<String>' in type cast - flutter/dart
[英]Flutter/Dart: Type 'int' is not a subtype of type 'String' in type cast
我已经看过这个问题的答案,但没有一个涵盖我似乎有的情况(由于缺乏理解答案,我可能错了)。 因此,我应该问一下。
我已经用 Postman 用相同的参数和数据测试了 API,它工作得很好。
onPressed: ()async {
print('Counter: $counter, Type: $type, Phone: $phone, Token: $token');
print(counter.runtimeType);
if(counter != 0){
print('above post now');
var response = await http.post(
acceptPass, //final URL in string
body:{
'type': type, //string
'phone': phone, //string
'qty': counter //int. Tried sending as string too, didn't work.
},
headers: {
'token': token //string
}
);
print("something with the response here ");
print(response.statusCode);
}
},
没有一个值为空,我确实将它们打印出来并在将它们放入 post 方法之前检查它们。 我还尝试将每个变量类型转换为字符串,然后将其发布,但无济于事。
body
必须是Map<String, String>
。 因此,您需要先将int
转换为字符串,然后再将其放入地图。
在counter
之后添加.toString()
。
要让编译器帮助您进行类型检查,您可以按如下方式显式键入映射。
body: <String, String>{
'fieldName': 'fieldValue',
//etc
您检查过acceptPass
吗? 这是您唯一不发表评论的人。 应该是一个URL。 https://pub.dev/packages/http
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.