android - 如何强制关闭 GetX Snackbar?
问题描述
我Get.snackbar()
用来展示连接到 API 的过程。我不明白如何以编程方式关闭小吃店?我有以下代码:
@override
Future<List> getImportantData(String inputData) async {
try {
final token = basicApiAuthString;
print("requesting API with the following request: $inputData");
Get.snackbar(
"Requesting very important data...",
"",
duration: 60.seconds, // it could be any reasonable time, but I set it lo-o-ong
snackPosition: SnackPosition.BOTTOM,
showProgressIndicator: true,
isDismissible: true,
backgroundColor: Colors.lightGreen,
colorText: Colors.white,
);
List importantData = await _client.requestAPI(basicAuthString: token, body: inputData);
.then((importantData) {
// Here I need to dismiss the snackbar I am still showing to user
print("I want to close snackbar here but I don't know how to do that!");
// Then I return data for future processing...
return importantData;
});
return importantData;
} on DioError catch (error) {
// Here I handle all possible API errors... Also I want to close snackbar here as well.
}
} // getImportantData() function.
我需要考虑以下几点:
- 应用程序可能会在请求期间关闭并再次打开(因此小吃店可能会关闭,但我可以通过状态回调知道它(未显示)
- 用户在请求期间可能会关闭小吃店
- 请求可能在毫秒内完成
- 可能存在 API 错误,
.then()
部分将永远不会被执行。
所以,我需要一些外部方法来关闭零食。Navigator.of(context).hide...
不是我使用 GetX 的解决方案。
———— PS:这里是snackbar的定义,帮我澄清一下:
https://pub.dev/documentation/get/3.4.2/route_manager/GetNavigation/snackbar.html
解决方案
GetX 已经有一个名为closeAllSnackbars()
and的方法closeCurrentSnackbar()
。
所以closeAllSnackbars()
用来关闭所有打开的 Snackbars。
Get.closeAllSnackbars();
要关闭当前活动的 snakbar,只需调用closeCurrentSnackbar()
.
Get.closeCurrentSnackbar();
您还可以通过调用来查看小吃店是否打开/活动的天气isSnackbarOpen()
。
Get.isSnackbarOpen();
推荐阅读
- angular - 是否可以在同一端口上托管 Angular 8 构建副本和 Slim/Lumen PHP 框架以避免 apache 服务器中的 CORS 问题
- matlab - 如何加快在服务器上的 VM 上运行非常缓慢的 Matlab 矩阵乘法?
- javascript - 希望 react-hamburger-menu 图标位于与汉堡包图标具有相同行为的容器中
- mysql - Mysql在安装过程中不会启动服务器
- angular - 注入服务时无法解析组件的所有参数
- node.js - 未加载样式表,即使已配置静态
- python - vtkXMLPolyData Writer/Reader 不可逆
- java - 调用 Oracle 存储过程抛出原因:java.sql.SQLException:无效的列类型:1111
- google-cloud-platform - 从 Pub/Sub 订阅中提取的 Google Cloud Function 引发异常 - 已超过截止日期
- javascript - React - 如何从 JSON 文件呈现内容?