How to Close Flutter application Programmatically?

To close the Flutter application Programmatically we need to use the below code

 

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

 

Example :

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  GlobalKey _scaffold_key=new GlobalKey();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,

        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("First Screen"),
        ),
        key: _scaffold_key,
        body: Container(
          child: Center(
            child: RaisedButton(onPressed: (){
             Navigator.push(_scaffold_key.currentContext, MaterialPageRoute(builder: (con){
               return Scaffold(
                 appBar: AppBar(
                   title: Text("Second Screen"),
                 ),
                 body: Container(
                   child: Center(
                     child: RaisedButton(onPressed: (){
                       SystemChannels.platform.invokeMethod('SystemNavigator.pop');
                     },child: Text("Close App"),),
                   ),
                 ),
               );
             }));
            },child: Text("Second Screen"),),
          ),
        ),
      ),
    );
  }
}


 


Flutter Questions

Android Questions

Python Questions

Java Questions