Dirk
Dirk

Reputation: 3391

Flutter how to programmatically exit the app

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

Upvotes: 244

Views: 233888

Answers (19)

Navas Shareef
Navas Shareef

Reputation: 131

use exit(0) in your click functions

InkWell(
  onTap:(){
       exit(0);
         }
      )

Upvotes: 0

George Freire
George Freire

Reputation: 47

Dev my app

onPressed: () {
                    Future future = SessionManager().destroy();
                    future.then((value) {
                      if (value) {
                        if (Platform.isAndroid) {
                          SystemNavigator.pop();
                        } else if (Platform.isIOS) {
                          exit(0);
                        }
                      }
                    });
                  },

Upvotes: 0

mashood pangat
mashood pangat

Reputation: 31

you can programmatically close the application by calling SystemNavigator.pop()

Upvotes: 2

TITOUAH YACINE
TITOUAH YACINE

Reputation: 1

This is a working way to exit the application:

 static Future<void> pop({bool? animated}) async {
     await SystemChannels.platform.invokeMethod<void> 
     ('SystemNavigator.pop',animated);
 }

Upvotes: 0

omega_mi
omega_mi

Reputation: 718

Exit app. Remember, the animation only works in iOS.

import 'package:flutter/services.dart';

static Future<void> exitApp({bool? animated}) async {
  await SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop', animated);
}

Flutter 3.16.4

Upvotes: 0

Anandh Krishnan
Anandh Krishnan

Reputation: 6022

You can programmatically exit the app by calling the SystemNavigator.pop() method. This method is provided by the services package in Flutter, and it allows you to exit the current app and return to the home screen or the previous app.

import 'package:flutter/services.dart';

void exitApp() {
  SystemNavigator.pop();
}

For iOS

import 'package:flutter/services.dart';

void exitApp() {
  if (Platform.isIOS) {
    SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }
}

This code uses SystemChannels.platform.invokeMethod to invoke the platform-specific method for exiting the app on iOS. It essentially replicates the behavior of pressing the home button, and it's a less abrupt way to navigate away from your app.

Please use this method with caution and only when it's absolutely necessary, as unexpected app exits can lead to a poor user experience on iOS.

Upvotes: 0

Shah Suhail
Shah Suhail

Reputation: 11

You can use SystemNavigator.pop();

Upvotes: 1

MeLean
MeLean

Reputation: 3441

I created a mixin for the closing like this:

import 'dart:io';
import 'package:flutter/services.dart';

mixin AppCloser {
  void closeApp() {
    if (Platform.isAndroid) {
      SystemNavigator.pop();
    } else if (Platform.isIOS) {
      exit(0);
    }
  }
}

Then I just add it on the Page like this:

class _HomePageState extends State<HomePage> with AppCloser {
    ...

    child: ElevatedButton(
        onPressed: () => { closeApp() },
        child: Text('Close'),
    ),

    ...   
}

Upvotes: 5

Shirsh Shukla
Shirsh Shukla

Reputation: 5993

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

Upvotes: 24

CopsOnRoad
CopsOnRoad

Reputation: 268304

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

Upvotes: 218

Hasan A Yousef
Hasan A Yousef

Reputation: 24988

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

Upvotes: 242

Abir Ahsan
Abir Ahsan

Reputation: 3059

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

Upvotes: 2

ali sampson
ali sampson

Reputation: 467

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

Upvotes: 1

MatraxDilgil
MatraxDilgil

Reputation: 29

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

      }
    }

Upvotes: 2

pinkfloyd
pinkfloyd

Reputation: 255

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!

Upvotes: 10

Ayan Bhattacharjee
Ayan Bhattacharjee

Reputation: 515

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

Upvotes: 12

Herman
Herman

Reputation: 135

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

Upvotes: 3

Blasanka
Blasanka

Reputation: 22437

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.

Upvotes: 52

Collin Jackson
Collin Jackson

Reputation: 116828

You can do this with SystemNavigator.pop().

Upvotes: 56

Related Questions