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

6
  • 1
    Maybe you need parenthesis SystemNavigator.pop() Commented Nov 3, 2019 at 13:30
  • 1
    Wouldn't you just use Navigator.Pop()? Commented Nov 3, 2019 at 13:30
  • I want to exit APP Commented Nov 3, 2019 at 13:38
  • check this answers stackoverflow.com/questions/45109557/… Commented Nov 3, 2019 at 13:58
  • Possible duplicate of Flutter how to programmatically exit the app Commented Nov 3, 2019 at 14:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I NEED TO IMPORT flutter/services.dart except this everything is ok
doesnot work on ios simulator. doesnot test on real iphone.
@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."
What's the advantage of SystemChannels.platform.invokeMethod('SystemNavigator.pop') over SystemNavigator.pop();?
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.
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();

Comments

0

I found a quick and simple solution for cases when you use the Add-To-App approach, and you need to open a modal full screen with FlutterViewController.

On iOS side just create an empty-controller:

class EmptyViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        guard let navController = self.navigationController else { return }
        // here is a trick
        navigationController.dismiss(animated: true) 
    }
}

// how to launch from native UIViewController
func showModalFullscreen() {
  let myFlutterViewController = MyFlutterViewController()
  let navController = UINavigationController()
  navController.setViewControllers([EmptyViewController(), myFlutterViewController], animated: false)
  present(navController, animated: true)
}

So, whenever you call from Flutter:

SystemNavigator.pop();

It will close the opened UINavigationController on the previous step.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.