Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振调用Navigator.pop内部异步函数_Flutter_Dart - Fatal编程技术网

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

Flutter 颤振调用Navigator.pop内部异步函数,flutter,dart,Flutter,Dart,我有一个异步函数,按下按钮时会调用它。 此函数执行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, "Cont

我有一个异步函数,按下按钮时会调用它。 此函数执行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();

  }

}
服务器上的值得到更新,printsuccess,但应用程序不会弹出到上一个屏幕

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

  }

}
所以我的问题是,当从异步函数调用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;
  }
}
您必须使用.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;
  }
}

如果您使用MaterialApp两次,也可能发生这种情况,如果可以,请发布整个代码。我不认为这是异步问题,可能是其他问题。您可以添加更多与此相关的代码。如果您使用MaterialApp两次,也可能会发生这种情况,如果可以,请发布整个代码。我认为这不是异步问题,可能是其他问题。您可以添加更多与此相关的代码吗。