flutter - 如何避免在颤动中与 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 请求。我想知道如何控制这个错误,因为此时应用程序死了。
我该怎么做?
解决方案
要捕获异常,您需要用 try/catch 块包装导致它的行:
try {
var response = http.post(url);
}
catch(exception){
print(exception);
}
推荐阅读
- javascript - 刷新页面时,Reactjs 道具不会填充字段
- package.json - 如何在不考虑 package.json 版本范围的情况下将包升级到最新版本?
- django - Django apache2 url直接没有基本url
- javascript - 新的 FB UI 元素提升/降级做什么
- machine-learning - SVM 加载的训练模型的准确率明显降低;我们也需要保存 TokenCountVectorizer 吗?
- symfony - 重置 Symfony 会话生命周期/用户活动超时
- ios - iOS:我收到一条错误消息“请求的属性表达式的地址”
- pandas - Kaggle 文件汇总错误(Santander Value Prediction Challenge)
- c# - 蜡工具包。如何使用 $(env.windir)?
- asp.net-core - Unobtrusive Ajax lib 不发送 XmlHttpRequest 标头