How to forcefully close GetX Snackbar?
Solution 1
Use Get.back() to close the snack bar.
If you want the user to close snack bar, You can use Get.back
either in mainButton or onTap
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,
mainButton: TextButton(
onPressed: Get.back,
child: const Text(
"Close"
)));
);
If debugLocked issue arise on close, use SchedulerBinding
SchedulerBinding.instance!.addPostFrameCallback((_) {
// Your Get Snackbar
}):
Solution 2
GetX has already a method called closeAllSnackbars()
and closeCurrentSnackbar()
.
So use closeAllSnackbars()
to close all open Snackbars.
Get.closeAllSnackbars();
And to close the current active snakbar simply call closeCurrentSnackbar()
.
Get.closeCurrentSnackbar();
You can also check weather the snackbar is open/active or not by calling isSnackbarOpen()
.
Get.isSnackbarOpen();
Solution 3
Use this method to close the current Snackbar immediately
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Constantine Kurbatov
AI/ML specialist in Fintech, entrepreneur and Data Science enthusiast, holding Master in Computer Science and Master in Finance degrees.
Updated on January 01, 2023Comments
-
Constantine Kurbatov over 1 year
I am using
Get.snackbar()
to show the process of connection to API. I don't understand how programmatically close a snackbar? I have the following code:@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.
I need to take into account, the following:
- app may be closed and open again during the request (so the snackbar may be closed, but I can know it by status callback (not shown)
- The snackbar may be dismissed by the user during request
- request may be completed in milliseconds
- There are possible API errors and
.then()
portion will never be executed.
So, I need some external way to close the snack.
Navigator.of(context).hide...
is not the solution as I use GetX.————— PS: Here is the definition of snackbar that doesn't help me to clarify:
https://pub.dev/documentation/get/3.4.2/route_manager/GetNavigation/snackbar.html
-
Constantine Kurbatov over 2 yearsHmm... tried the following to no avail:
final X = Get.context;
andScaffoldMessenger.of(X!).removeCurrentSnackBar();
Looks like in the GetX environment there is another way should be used. -
Meet Prajapati over 2 yearsyou have to pass the build context not the "Get.context"
-
rajagopalx over 2 yearsAlso try
Get.back(closeOverlays: true);