Reputation: 3391
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
Reputation: 131
use exit(0)
in your click functions
InkWell(
onTap:(){
exit(0);
}
)
Upvotes: 0
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
Reputation: 31
you can programmatically close the application by calling SystemNavigator.pop()
Upvotes: 2
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
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
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
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
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
Reputation: 268304
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
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
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
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
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
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
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
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
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
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