Flutter: How to Programatically Exit App ( IOS | Android) [Solved]

Hi,

You are working on your project.

Now! a requirement is given to you that a sudden button should close the app.

And you are a flutter developer. You just start thinking about how you can solve this issue.

Believe me,

This is super simple.

And

Today’s short tutorial will help you to exit your flutter app programmatically by pressing the button.

So, Let’s get started

I know some of you are looking for code snippet.

So, here you get It

On android Device

SystemNavigator.pop();

On IOS Device

exit(0);

Note: Using this on IOS device will may violate Appstore policy. So, make sure you are using it wisely.

Now! you get the code.

But

I want to discuss it in detail.

So, here we go

Let’s create a demo app with an ElevatedButton at the center of the page. And I want to close my app when I click that button.

Import these files.

import 'dart:io';

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

We use dat:io, material and services files from the flutter framework.

This is the main function.

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

This is the MyApp widget.

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Code with Hussain'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {},
            child: const Text('Close'),
          ),
        ),
      ),
    );
  }
}

This button is the focus point in this conversation.

ElevatedButton(
            onPressed: () {},
            child: const Text('Close'),
          ),

We use this onPressed parameter of this ElevatedButton. We pass an anonymous function.

Create some super simple logic for platform checking.

I will use the if statement in this way.

If current device is android (Platform.isAndroid) then execute this code and if device is IOS (Platform.isIOS) then execute that code.

Let’s do it.

onPressed: () {
              if (Platform.isAndroid) {
                SystemNavigator.pop();
              } else if (Platform.isIOS) {
                exit(0);
              }
            }

Here SystemNavigator.pop() is using from the services file.

And exit(0) is used from the dart:io file.

Full Code

import 'dart:io';

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Code with Hussain'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              if (Platform.isAndroid) {
                SystemNavigator.pop();
              } else if (Platform.isIOS) {
                exit(0);
              }
            },
            child: const Text('Close'),
          ),
        ),
      ),
    );
  }
}

Remarks

I hope now you have a clear idea about exiting the android or IOS apps in flutter programmatically. Thanks

Default image

Hussain Humdani

while ( ! ( succeed = try() ) );