首页 > 解决方案 > 如何避免在颤动中与 api 的连接错误?(断点类型出现在VS代码中)

问题描述

我故意造成这个错误。

在此处输入图像描述

  var response = await http.post(url,headers: {"Content-Type": "application/x-www-form-urlencoded"},body: args); //die here
  //not continues
  print(response);

我已经关闭了我的服务器的执行,所以无法执行 web 请求。我想知道如何控制这个错误,因为此时应用程序死了。

我该怎么做?

标签: flutter

解决方案


要捕获异常,您需要用 try/catch 块包装导致它的行:

try {
   var response = http.post(url);
}
catch(exception){
  print(exception);
}

推荐阅读