Here is my code. I used SystemNavigator.pop but it says undefined name SystemNavigator
All I want is to exit the app using OnTap
class AppDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
_createHeader(),
_createDrawerItem(
icon: Icons.home,
text: 'Home',
onTap: () => Navigator.of(context).pop()),
Divider(),
_createDrawerItem
(icon: Icons.exit_to_app, text: 'Exit app',
onTap: () => SystemNavigator.pop),
),
],
),
);
}
What is the proper method to add SystemNavigator.pop
in this case? Please help me if you know any method to add SystemNavigator.pop
You might be missing an import containing SystemNavigator, so add:
import 'package:flutter/services.dart';
You are missing a parenthesis, so, correct SystemNavigator.pop
to
SystemNavigator.pop()
Alternatively, you can use SystemChannels
:
SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
instead of SystemNavigator.pop()
inside onTap
. Refer flutter documentation here: Link
Flutter documentation in the above link says:
Instructs the system navigator to remove this activity from the stack and return to the previous activity.
On iOS, calls to this method are ignored because of Apple's human interface guidelines state that applications should not exit themselves.
This method should be preferred over calling dart:io's exit method, as the latter may cause the underlying platform to act as if the application had crashed.
Never use exit(0)
if you are planning to launch the app on the Apple app store as Apple Human Interface guidelines strongly discourage to exit the app programmatically. Refer to this iOS documentation archive.
The above link says:
Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.
Flutter documentation on exit() says:
Exit the Dart VM process immediately with the given exit code.
This does not wait for any asynchronous operations to terminate. Using exit is therefore very likely to lose data.