Open In App

Flutter – Programmatically Exit From the Application

Last Updated : 23 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to programmatically close a Flutter application. 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 the user may think that the app just got crashed. A sample video is given below to get an idea about what we are going to do in this article.

How to Use?

Using System Navigator:

SystemChannels.platform.invokeMethod('SystemNavigator.pop');

Using Exit:

exit(0);

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the material package

Adding material package that gives us the important functions and calls the runApp method in the main function that will call our application.

import 'package:flutter/material.dart';
void main() {
  runApp(RunMyApp());
}

Step 3: Creating Stateless Widget

Now we have to make a stateless widget because our application does not go to change its state and then return the materialApp widget which allows us the set the title and theme and many more.

class RunMyApp extends StatelessWidget {
  const RunMyApp({super.key});
  
  @override
  Widget build(BuildContext context) {
      return MaterialApp();
  }
}

Step 4: Creating Scaffold Widget

Give the home property and there can be a scaffold widget that has the property of AppBar and body. Further AppBar allows us to give the title of AppBar, color, leading, and trailing icon. And the body takes the widget or class.

home: Scaffold(
  appBar: AppBar(
      title: Text('Exit from the App'),
  ),
  body: Center();
),

Step 5: Center widget has a column as a child. Column allows us to set its children, Two elevated Button that calls the respected method cliseAppUsingSystemPop and closeAppUsingExit.

Final Code

Dart




import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
  
void main() {
  runApp(RunMyApp());
}
  
class RunMyApp extends StatelessWidget {
  const RunMyApp({super.key});
  void closeAppUsingSystemPop() {
    SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }
  
  void closeAppUsingExit() {
    exit(0);
  }
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.green),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Exit From the App'),
        ),
        body: Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  closeAppUsingSystemPop();
                },
                child: Text('Exit using System Navigator')),
            ElevatedButton(
                onPressed: () {
                  closeAppUsingExit();
                },
                child: Text('Exit using Exit 0')),
          ],
        )),
      ),
    );
  }
}


Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads