Open In App

How to Use SystemNavigator.pop() to Exit App in Flutter?

Last Updated : 30 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In many applications, we need force to close the entire application. So in this article, we are going to do the same. We can use the SystemNavigator.pop() to exit the application. There is no need for additional packages.

How to Use?

Import the library

First, we need to import the service package.

Dart




import 'package:flutter/services.dart';


Syntax

Dart




SystemNavigator.pop();


We will use the Text Button at to call the Navigator pop method that will end the user screen.

Code Example 

Dart




import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
  
// main app that calls the our main class
void main() {
  runApp(ExitAppRun());
}
  
class ExitAppRun extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp( 
     debugShowCheckedModeBanner:false,
     title: 'ExitApp',
     // scaffold with appbar property
     home: Scaffold(  
      // appbar with its title
      appBar: AppBar( 
        title: Text('Exit App'),
      ),
      // in the center of the body 
      // we have a button as a child
      body:Center( 
        child:TextButton.icon(
          icon:Icon(Icons.close),
          label:Text('Exit App'), 
          // onpressed we call the pop method, 
          // that will pop the current screen.
          onPressed:(){ 
            SystemNavigator.pop();
          },             
        ),
       ),
     ),
    );
  }
}


Output UI

Output UI

 

 

Code Explanation

  • Main Is The Principal Method Used To Run ExitAppRun Class When The Page Is Loaded.
  • Creating Class ExitAppRun, Stateless As There Is No Data Change After The Page Is Loaded (No State Change). 
  • As Flutter Is Based On Widgets, We Need To Create One.
  • Creating A Material App That Takes Scaffold Allowing Us To Use AppBar And Body.
  • As An AppBar It Has An Simple Title.
  • As An Body, It Takes A Centered Button Once Pressed Will Close The App!

Output 



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

Similar Reads