Flutter how to programmatically exit the app

FlutterDart

Flutter Problem Overview


How can I programmatically close a Flutter application. I've tried popping the only screen but that results in a black screen.

Flutter Solutions


Solution 1 - Flutter

Below worked perfectly with me in both Android and iOS, I used exit(0) from dart: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 Preferable solution is:

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

As described here

Solution 2 - Flutter

For iOS

SystemNavigator.pop(): Does NOT WORK

exit(0): Works but Apple may SUSPEND YOUR APP

Please see:
https://developer.apple.com/library/archive/qa/qa1561/_index.html


For Android

SystemNavigator.pop(): Works and is the RECOMMENDED way of exiting the app.

exit(0): Also works but it's NOT RECOMMENDED as it terminates the Dart VM process immediately and user may think that the app just got crashed.

Please see:
https://api.flutter.dev/flutter/services/SystemNavigator/pop.html

Solution 3 - Flutter

You can do this with SystemNavigator.pop().

Solution 4 - Flutter

Answers are provided already but please don't just copy paste those into your code base without knowing what you are doing:

If you use SystemChannels.platform.invokeMethod('SystemNavigator.pop'); note that doc is clearly mentioning:

> 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 Apple's human > interface guidelines state that applications should not exit > themselves.

You can use exit(0). And that will terminate the Dart VM process immediately with the given exit code. But remember that doc says:

> This does not wait for any asynchronous operations to terminate. Using > exit is therefore very likely to lose data.

Anyway the doc also did note SystemChannels.platform.invokeMethod('SystemNavigator.pop');:

> 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.

So, keep remember what you are doing.

Solution 5 - Flutter

you can call this by checking platform dependent condition. perform different action on click for android and ios.

import 'dart:io' show Platform;
import 'package:flutter/services.dart';
    
RaisedButton(
  onPressed: () {
    if (Platform.isAndroid) {
      SystemNavigator.pop();
    } else if (Platform.isIOS) {
      exit(0);
    }
  },
  child: Text("close app")
)

Solution 6 - Flutter

I prefer using

 Future.delayed(const Duration(milliseconds: 1000), () {
        SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      });

Although, exit(0) also works but the app closes abruptly and doesn't look nice, kind of seems that the app has crashed.

 Future.delayed(const Duration(milliseconds: 1000), () {
       exit(0);
      });

Solution 7 - Flutter

the only solution I have seen so far, accepted in both stores is:

for android:

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

for ios:

there is no possibility of closing the app BUT you can move it to the background with https://pub.dev/packages/minimize_app like:

MinimizeApp.minimizeApp();

enjoy!

Solution 8 - Flutter

This worked for me;

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),
      ), 
  );
}

Solution 9 - Flutter

I am calling _getOutOfApp function, this is not package;

    void _getOutOfApp {

      if (Platform.isIOS) {

        try {
          exit(0); 
        } catch (e) {
          SystemNavigator.pop(); // for IOS, not true this, you can make comment this :)
        }

      } else {

        try {
          SystemNavigator.pop(); // sometimes it cant exit app  
        } catch (e) {
          exit(0); // so i am giving crash to app ... sad :(
        }

      }
    }

Solution 10 - Flutter

This worked for me. Create a function, and in your onPressed function just call this function. Since exit(0) is not recommended by apple for iOS apps, I opted for this library here: https://pub.dev/packages/minimize_app. Below is the code snippet:

void closeApp(){
   if(Platform.isAndroid){
       SystemChannels.platform.invokeMethod('SystemNavigator.pop');            
   }else{
       MinimizeApp.minimizeApp();
   }
}

Solution 11 - Flutter

You can use SystemNavigator.pop(); for Android and for iOS use exit(0);

here is example-

if (Platform.isIOS) {
    exit(0);
    } else {
    SystemNavigator.pop();
            }

don't forget import import 'dart:io'; on top

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheo BouwmanView Question on Stackoverflow
Solution 1 - FlutterHasan A YousefView Answer on Stackoverflow
Solution 2 - FlutterCopsOnRoadView Answer on Stackoverflow
Solution 3 - FlutterCollin JacksonView Answer on Stackoverflow
Solution 4 - FlutterBlasankaView Answer on Stackoverflow
Solution 5 - Fluttershirsh shuklaView Answer on Stackoverflow
Solution 6 - FlutterAyan BhattacharjeeView Answer on Stackoverflow
Solution 7 - FlutterpinkfloydView Answer on Stackoverflow
Solution 8 - FlutterHermanView Answer on Stackoverflow
Solution 9 - FlutterMatraxDilgilView Answer on Stackoverflow
Solution 10 - Flutterali sampsonView Answer on Stackoverflow
Solution 11 - FlutterAbir AhsanView Answer on Stackoverflow