How can I programmatically close a Flutter application. I've tried popping the only screen but that results in a black screen.
19 Answers
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
3 Comments
t work for me... Im trying to add an exit button to CupertinoTabScafold. With ()=>exit(0), it gives a blank screen with tabbar.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
11 Comments
SystemNavigator.pop() is not working under Android emulator. The app is still working in background. So I am not sure if it will be working properly on real device.exit(0) for your need.SystemNavigator.pop(...)WillPopScope....You can do this with SystemNavigator.pop().
10 Comments
exit(0); does workSystemNavigator.pop is ignored because Apple's human interface guidelines state that applications should not exit themselves. On Android, it should work, and is preferred over calling dart:io's exit method. Consider filing an issue: github.com/flutter/flutter/issues/newAnswers 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.
3 Comments
SystemChannels.platform.invokeMethod('SystemNavigator.pop') and not simply SystemNavigator.pop();?invokeMethod we are directly calling native android pop method. At that time Im writing may be flutter method pop did not exist. may be the SystemNavigator.pop(); invoke native method to exit (Check the method source code, I havent checked it).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")
)
Comments
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);
});
Comments
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!
Comments
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'),
),
...
}
Comments
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),
),
);
}
Comments
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 :(
}
}
}
Comments
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();
}
}
Comments
You can use SystemNavigator.pop();
1 Comment
use exit(0) in your click functions
InkWell(
onTap:(){
exit(0);
}
)
1 Comment
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.