Flutter how to programmatically exit an app

How to programmatically close a Flutter application. I tried to pop up a single screen, but the result is a black screen.

+38


source to share


4 answers


Below worked great for me both on Android

and on iOS

, I used exit(0)

fromdart:io

import 'dart:io';

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new ... (...),
          floatingActionButton: new FloatingActionButton(
            onPressed: ()=> exit(0),
            tooltip: 'Close app',
            child: new Icon(Icons.close),
          ), 
    );
  }

      

UPDATE Jan 2019 The preferred solution is:



SystemChannels.platform.invokeMethod('SystemNavigator.pop');

      

As described here

+63


source


You can do this with . SystemNavigator.pop()



+21


source


Answers have already been provided, but please don't copy and paste them into your codebase without knowing what you are doing:

If you are using SystemChannels.platform.invokeMethod('SystemNavigator.pop');

, please note that doc explicitly mentions:

Instructs the system navigator to remove this action from the stack and return to the previous action.

On iOS, calls to this method are ignored because Apple human The interface manual states that apps should not quit by themselves.

You can use exit(0)

. And this will immediately abort the Dart VM process with the given exit code. But remember what the doc says:

It does not wait for any asynchronous operations to complete. Using therefore, the output can result in data loss.

In any case, the document also noted SystemChannels.platform.invokeMethod('SystemNavigator.pop');

:

This method should be preferred over calling the dart: io method, as the latter can cause the main framework to act as if the Application crashed.

So, remember what you are doing.

+8


source


For iOS

SystemNavigator.pop()

: DOES NOT WORK

exit(0)

: Works, but Apple MAY SUPPORT YOUR APPLICATION because it conflicts with Apple Human Interface guidelines for programmatically exiting an application.


For Android

SystemNavigator.pop()

: works and is the RECOMMENDED way to exit the application.

exit(0)

: also works, but it is NOT RECOMMENDED as it exits the Dart VM process immediately and the user might think the app has just crashed.

+4


source







All Articles