I'm encountering an issue where calling Navigator.pop() twice from showDialog() leads to a blank screen. I'm using the auto_router package. How can I resolve this?
PopScope widget
PopScope(
canPop: false,
onPopInvoked: (didPop) {
log('on click back button ${MainApp.router.stack.toList()}');
log('didPop: $didPop');
if (didPop) {
log('after pop ${MainApp.router.stack.toList()}');
return;
}
_showBackDialog();
},
showbackdialog function
void _showBackDialog() {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
alignment: Alignment.bottomCenter,
title: const Text('Are you sure?'),
content: const Text(
'Are you sure you want to leave this page?',
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Nevermind'),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Leave'),
onPressed: () async {
Navigator.pop(context);
Navigator.pop(context);
},
),
],
);
},
);
}
Even when the stack route is empty, the route keeps popping instead of exiting the app.
as in your case you need to exit the app right, you can put
exit(0)
on the onpressed, to close the app
so rather for pop until certain screen would be used the popUntil, and to close the app you can use it like this
onPressed: () async {
Navigator.pop(context);
exit(0);
},
or you can just use the
onPressed: () async {
exit(0);
},
when you use Navigator.pop(context)
multiple times, it's just popping routes off the navigation stack, not necessarily closing the entire app. The app will only exit if there are no more routes left on the stack and the back button is pressed (on Android) or if you explicitly call exit(0)
. If you're popping all routes but the app is still stuck on a blank screen, there might be something else going on, like a persistent background process or a platform-specific issue
as you mention in comment
Yeah, another solution I found is to use SystemNavigator.pop();. However, both accomplish my goal of exiting the app. I'm not sure which one is considered best practice.
you found, SystemNavigator.pop()
work well in your case. well in your case i still prefer you to used the exit(0)
. why? here's the explanation
Both SystemNavigator.pop()
and exit(0)
serve different purposes, and why exit(0) is "better" on your specific use case
SystemNavigator.pop()
: This method is specifically designed to mimic the behavior of the system's back button. It will pop the current route off the navigation stack, similar to pressing the back button on Android or swiping back on iOS. It's usually used when you want to navigate back within your app, maintaining the app's expected behavior.
exit(0)
: This method immediately terminates the Dart VM process, effectively closing the entire app. It's a more forceful way to exit the app and should be used sparingly. It's typically used when you want to completely close the app, bypassing any navigation stack or cleanup procedures.
In general, SystemNavigator.pop()
is preferred for standard navigation within your app, while exit(0)
is reserved for situations where you need to forcefully close the app, such as in certain error-handling scenarios or when exiting the app entirely.