如何强制关闭GetX Snackbar?

Con*_*tov 4 android dart snackbar flutter flutter-getx

我用来Get.snackbar()显示连接到 API 的过程。\n我不明白如何以编程方式关闭小吃栏?\n我有以下代码:

\n
@override\n  Future<List> getImportantData(String inputData) async {\n    try {\n      final token =  basicApiAuthString;\n      print("requesting API with the following request: $inputData");\n      Get.snackbar(\n          "Requesting very important data...",\n          "",\n          duration: 60.seconds, // it could be any reasonable time, but I set it lo-o-ong\n          snackPosition: SnackPosition.BOTTOM,\n          showProgressIndicator: true,\n          isDismissible: true,\n          backgroundColor: Colors.lightGreen,\n          colorText: Colors.white,\n        );\n      List importantData = await _client.requestAPI(basicAuthString: token, body: inputData);\n          .then((importantData) {\n              // Here I need to dismiss the snackbar I am still showing to user\n              print("I want to close snackbar here but I don\'t know how to do that!");\n              // Then I return data for future processing... \n              return importantData;\n                });\n\n      return importantData;\n    } on DioError catch (error) {\n      // Here I handle all possible API errors... Also I want to close snackbar here as well.\n  \n    }\n } // getImportantData() function.\n
Run Code Online (Sandbox Code Playgroud)\n

我需要考虑以下因素:

\n
    \n
  1. 应用程序可能会在请求期间关闭并再次打开(因此小吃栏可能会关闭,但我可以通过状态回调知道它(未显示)
  2. \n
  3. 用户在请求期间可能会关闭小吃栏
  4. \n
  5. 请求可能在几毫秒内完成
  6. \n
  7. 可能存在 API 错误并且.then()部分永远不会被执行。
  8. \n
\n

所以,我需要一些外部的方式来关闭零食。Navigator.of(context).hide...不是解决方案,因为我使用 GetX。

\n

\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\nPS:这是snackbar的定义,但不能帮助我澄清:

\n

https://pub.dev/documentation/get/3.4.2/route_manager/GetNavigation/snackbar.html

\n

muf*_*zmi 12

GetX 已经有一个名为closeAllSnackbars()和的方法closeCurrentSnackbar()

因此,请使用closeAllSnackbars()关闭所有打开的 Snackbars。

Get.closeAllSnackbars();
Run Code Online (Sandbox Code Playgroud)

要关闭当前活动的 Snakbar,只需调用closeCurrentSnackbar().

Get.closeCurrentSnackbar();
Run Code Online (Sandbox Code Playgroud)

您还可以通过致电 来检查小吃店是否打开/活动isSnackbarOpen()

Get.isSnackbarOpen();
Run Code Online (Sandbox Code Playgroud)