flutter official logo

How to Exit a Flutter Application

In some apps, especially utility or kiosk-style apps, you might want to provide an option to exit the app programmatically. Here's how you can exit a Flutter application properly.

1. Use the SystemNavigator.pop()

This is the most common and recommended way to close the app on Android:

import 'package:flutter/services.dart';

SystemNavigator.pop();

This method works only on Android and will pop the current Flutter activity, effectively closing the app.

2. Use exit(0) from dart:io

If you want a more forceful exit, you can use this method:

import 'dart:io';

exit(0);

However, this is not recommended for production apps as it doesn't follow the platform's lifecycle rules. It's more suitable for testing or kiosk-type apps.

Which method should you use?

  • Use SystemNavigator.pop() for Android-friendly behavior.
  • Use exit(0) only when necessary, like in controlled environments.

On iOS, apps are not supposed to be closed programmatically by design. So these methods may not work or can cause your app to be rejected if misused.

Example with a button

ElevatedButton(
  onPressed: () {
    SystemNavigator.pop();
  },
  child: Text('Exit App'),
)

Always consider whether exiting the app manually is necessary. Mobile platforms are designed to manage app lifecycles automatically.