Flutter how to programmatically exit the app

Viewed 143200

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

11 Answers

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.

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")
)

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

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!

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

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 :(
        }

      }
    }

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

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

Related