flutter - 如何在颤振中创建套接字异常屏幕?
问题描述
在我的颤振项目中,当调用 API 时发生套接字异常时,我需要显示一些插图图像。我怎样才能做到这一点 ?
提前致谢
解决方案
这取决于您要在小部件树中显示的位置。一个简单的示例是将新屏幕推送到导航堆栈。您将需要在可能发生异常的函数中使用 BuildContext。
void someMethod(BuildContext context) {
try {
//some code that might throw an exception
} on Exception catch (_) {
Navigator.pushNamed(context, "Your illustration view");
}
}
另一个示例是根据布尔值将其添加到您的小部件树中。当抛出异常时,您将该 bool 设置为 true。
void someOtherMethod() {
try {
//some code that might throw an exception
} on Exception catch (_) {
setState(() {
hasThrownError = true;
});
}
}
在您的小部件树中使用它,如下所示:
bool hasThrownError = false;
Widget buildWidgetTree() {
return hasThrownError
? Text("This is where you can show your error illustration")
: Text("This is wher you can show your regular view");
}
推荐阅读
- c++ - 如何在 C++ 或 OpenCV SubMatrix 中的 2D 子向量上使用 OpenACC?
- python - 什么是在不涉及炮击的 python 中运行强盗的更简洁的方法
- python - 如何检查列表中是否存在整数或字符串
- azure - Azure CDN 终结点:为默认自定义域 mediaservice-inct.streaming.media.azure.net 启用 HTTPS
- ubuntu - CMake 错误:CMake 无法找到与“Unix Makefiles”对应的构建程序。CMAKE_MAKE_PROGRAM 未设置。在 Ubuntu 中
- c# - 如何使用 CreatedAtRoute() 方法响应发布请求
- reactjs - 为 Redux 操作设置 Typescript 接口(类型“...”没有调用签名)
- python - 通过拖动鼠标旋转对象pygame
- c - 使用 SDL2 的 C 程序不能用 clang 编译
- python - 如何由用户在 django 上查询 sql 以获得模型函数