How to Programmatically Exit iOS or Android App with Code in Flutter

Do you want to close your app automatically using code, or want to close the app when the user presses any button then read this guide to learn to exit your app programmatically. 

Close iOS App with Code:

SystemNavigator.pop(): This command does not work in iOS

exit(0): This command works on iOS but Apple may suspend your app because it's against Human Interface guidelines to exit the app programmatically.

Close Android App With Code:

SystemNavigator.pop(): This command works and is the recommended way of exiting the app in Android.

exit(0): This command also works but it is not recommended because it terminates the Dart VM process immediately and users may think that the app got crashed.

Full Dart Code Example:

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

void main() {
  runApp(MyApp()); 
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Test App",
        home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return Scaffold( 
        appBar: AppBar( 
          title: Text("Exit App"),
          backgroundColor: Colors.redAccent,
        ),
        body: Container( 
          child:Center( 
            child: ElevatedButton( 
              onPressed: (){
                  SystemNavigator.pop(); //for Android from flutter/services.dart
                  //exit(0) for both Android and iOS
              },
              child: Text("Exit App"),
            ),
          )
        )
    );
  }
}

Output Screenshot:

When you press the "Exit App" button, the app will close without pressing back button. This is the way you can programmatically close Android or iOS app using code in Flutter.

No any Comments on this Article


Please Wait...