[英]Flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast
I'm sending a post api request from my flutter App to update the data of user and getting this error我正在从我的 flutter 应用程序发送 api 请求以更新用户数据并收到此错误
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast [错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:类型'int'不是类型转换中类型'String'的子类型
my Code我的代码
_data() async {
setState(() {
data();
});
}
Future<bool> data() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;
http.Response response = await http.post(
"https://www.exmaple.com/api/user",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer $value'
},
body: {'key1': value1, 'key2': value2},
);
if (response.statusCode == 200) {
return true;
} else {
return false;
}
}
the above code works when i clicked on a button.当我点击一个按钮时,上面的代码有效。 See below code
看下面的代码
RaisedButton(
onPressed: () {
_data();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) => Home()),
(Route<dynamic> route) => false);
},
child: Text('Press Me',
style: TextStyle(fontSize: 15)),
),
See the ScreenShot of error Error ScreenShot查看错误的截图Error ScreenShot
here is error error in line 76这是第 76 行的错误错误
Make sure value1
and value2
is String
.确保
value1
和value2
是String
。
body: {'key1': value1.toString(), 'key2': value2.toString()},
Don't know which exactly line is throwing you error, but maybe try parsing it?不知道到底是哪一行给你带来了错误,但也许尝试解析它?
var value = int.tryParse(text);
Please use the Authentication as follows.请按如下方式使用身份验证。
'Authorization': 'Bearer '+$value.toString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.