getx
getx copied to clipboard
Get.back() not work after Get.snackbar
Describe the bug Use Get.snackbar() first in a component, and then continue to use Get.back(), neither of which takes effect
**Reproduction code
example:
class ServiceView extends GetView<ServiceController> {
const ServiceView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ServiceView'),
centerTitle: true,
),
body: Center(
child: ElevatedButton(
onPressed: () {
Get.snackbar("notice", "noticeContent");
Get.back();
},
child: const Text(
"notice and back",
),
),
),
);
}
}
To Reproduce Steps to reproduce the behavior:
- use
get create page service
create page - Add example code to the view page
Expected behavior Prompt information and return to the previous page
Screenshots
Flutter Version: 3.10.5
Getx Version: 4.6.5
Describe on which device you found the bug: ex: Chrome,android emulator, android device
Minimal reproduce code
ElevatedButton(
onPressed: () {
Get.snackbar("notice", "noticeContent");
Get.back();
},
child: const Text(
"notice and back",
),
),
@dengwei555
-
Get.back()
in this case mean you close "SnackBar" - Try to move
Get.back()
before show SnackBar like this
ElevatedButton(
onPressed: () {
Get.back();
Get.snackbar("notice", "noticeContent");
},
child: const Text(
"notice and back",
),
),
On an Apple phone, when the snackbar pops up, immediately sliding back to the previous page can also cause problems. So what should we do?
Agree with the last comment
As a workaround, we can use Get.until((route) => route== 'Name of your route');
Approach 1:
if you want to display snackbar and then want to navgate back to screen, you can this properties: snackbarStatus
Get.snackbar(
'Title',
'Message',
snackbarStatus: (status) {
if (status == SnackbarStatus.CLOSED) {
// ..
}
},
);
Approach 2: Closing a snackbar dynamically is a handy feature for when you need to return to a screen with open dialogues, bottom sheets, or snackbars. checkout this Reference
Get.back(closeOverlay = true / false) exists for this.