异步函数内部的Flutter调用Navigator.pop

问题描述 投票:0回答:1

我有一个异步功能,当按下按钮时会调用它。此功能会发出http放置请求,如果结果成功,则需要弹出上一个屏幕。

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();

  }

}

服务器上的值已更新,print(“成功”)),但应用程序未弹出到上一屏幕。

所以我的问题是,从异步函数调用时,Navigator类不能正常工作吗?

flutter dart
1个回答
0
投票

您必须使用.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;
}

}
© www.soinside.com 2019 - 2024. All rights reserved.