我有一个异步函数,按下按钮时会调用该函数。这个函数执行一个http put请求,如果结果成功我需要弹出到上一个屏幕。
void updateSurv() async{
http.Response result;
result = await http.put(
"http://10.0.2.2:8000/emergencies/${widget.id}/put",
body: {
"title" : titleController.text,
"Content" : contentController.text,
}
);
if(json.decode(result.body)["result"] == "Success"){
print("success");
Navigator.of(context).pop();
}
}
Run Code Online (Sandbox Code Playgroud)
服务器上的值会更新,print("success"),但应用程序不会弹出到上一个屏幕。
所以我的问题是,从异步函数调用时 Navigator 类不起作用吗?
您必须使用.then()函数来执行此类操作。
updateSurv().then((result) {
if (json.decode(result.body)["result"] == "Success") {
print("success");
Navigator.of(context).pop();
}
});
Future<dynamic> updateSurv() async {
try {
result = await http.put("http://10.0.2.2:8000/emergencies/${widget.id}/put", body: {
"title": titleController.text,
"Content": contentController.text,
});
return result;
} catch (e) {
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4505 次 |
| 最近记录: |