【发布时间】:2025-11-24 19:30:01
【问题描述】:
我正在从我的 Flutter 应用程序发送一个 post api 请求以更新用户的数据并收到此错误
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常: “int”类型不是类型转换中“String”类型的子类型
我的代码
_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;
}
}
当我单击一个按钮时,上面的代码有效。见下面代码
RaisedButton(
onPressed: () {
_data();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) => Home()),
(Route<dynamic> route) => false);
},
child: Text('Press Me',
style: TextStyle(fontSize: 15)),
),
查看错误Error ScreenShot的截图
这里是错误error in line 76
【问题讨论】:
标签: android android-studio flutter dart