handleRequestAppExit method

  1. @override
Future<AppExitResponse> handleRequestAppExit()
inherited

Handles any requests for application exit that may be received on the SystemChannels.platform method channel.

By default, returns ui.AppExitResponse.exit.

Not all exits are cancelable, so not all exits will call this function. Do not rely on this function as a place to save critical data, because you will be disappointed. There are a number of ways that the application can exit without letting the application know first: power can be unplugged, the battery removed, the application can be killed in a task manager or command line, or the device could have a rapid unplanned disassembly (i.e. it could explode). In all of those cases (and probably others), no notification will be given to the application that it is about to exit.

{@tool sample} This examples shows how an application can cancel (or not) OS requests for quitting an application. Currently this is only supported on macOS and Linux.

** See code in examples/api/lib/services/binding/handle_request_app_exit.0.dart ** {@end-tool}

See also:

Implementation

@override
Future<AppExitResponse> handleRequestAppExit() async {
  bool didCancel = false;
  for (final WidgetsBindingObserver observer in List<WidgetsBindingObserver>.of(_observers)) {
    if ((await observer.didRequestAppExit()) == AppExitResponse.cancel) {
      didCancel = true;
      // Don't early return. For the case where someone is just using the
      // observer to know when exit happens, we want to call all the
      // observers, even if we already know we're going to cancel.
    }
  }
  return didCancel ? AppExitResponse.cancel : AppExitResponse.exit;
}