6

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

raman raman
  • 1,655
  • 4
  • 22
  • 34

2 Answers2

15

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.

FutureJJ
  • 2,368
  • 1
  • 19
  • 30
  • I NEED TO IMPORT flutter/services.dart except this everything is ok – raman raman Nov 04 '19 at 01:34
  • 2
    doesnot work on ios simulator. doesnot test on real iphone. – Akbar Pulatov Apr 21 '21 at 12:35
  • 1
    @AkbarPulatov as mentioned in the answer Flutter ignores this command in iOS to comply with Apple's Human Interface guideline. "On iOS, calls to this method are ignored because of Apple's human interface guidelines state that applications should not exit themselves." – FutureJJ Apr 21 '21 at 13:35
  • What's the advantage of `SystemChannels.platform.invokeMethod('SystemNavigator.pop')` over `SystemNavigator.pop();`? – Csaba Toth May 28 '21 at 00:41
  • Does SystemNavigator.pop() wait for sync operation to complete ? Or my real question is there a way to move hte async operation to the background and close the app when it finishes. – Anirudh May 28 '21 at 03:10
0

Please refer to the following link https://api.flutter.dev/flutter/services/SystemNavigator/pop.html

If you are trying to link a flutter module to an existing iOS app, then you will have to do the following modification to the ViewController Class.

Change
class ViewController: UIViewController {}

to
class ViewController: FlutterViewController {}

This should work if you use SystemNavigator.pop();

Anoop
  • 1