exitApplication method

Future<AppExitResponse> exitApplication(
  1. AppExitType exitType, [
  2. int exitCode = 0
])

Exits the application by calling the native application API method for exiting an application cleanly.

This differs from calling dart:io's exit function in that it gives the engine a chance to clean up resources so that it doesn't crash on exit, so calling this is always preferred over calling exit. It also optionally gives handlers of handleRequestAppExit a chance to cancel the application exit.

The exitType indicates what kind of exit to perform. For ui.AppExitType.cancelable exits, the application is queried through a call to handleRequestAppExit, where the application can optionally cancel the request for exit. If the exitType is ui.AppExitType.required, then the application exits immediately without querying the application.

For ui.AppExitType.cancelable exits, the returned response value is the response obtained from the application as to whether the exit was canceled or not. Practically, the response will never be ui.AppExitResponse.exit, since the application will have already exited by the time the result would have been received.

The optional exitCode argument will be used as the application exit code on platforms where an exit code is supported. On other platforms it may be ignored. It defaults to zero.

See also:

Implementation

Future<ui.AppExitResponse> exitApplication(ui.AppExitType exitType, [int exitCode = 0]) async {
  final Map<String, Object?>? result = await SystemChannels.platform.invokeMethod<Map<String, Object?>>(
    'System.exitApplication',
    <String, Object?>{'type': exitType.name, 'exitCode': exitCode},
  );
  if (result == null ) {
    return ui.AppExitResponse.cancel;
  }
  switch (result['response']) {
    case 'cancel':
      return ui.AppExitResponse.cancel;
    case 'exit':
    default:
      // In practice, this will never get returned, because the application
      // will have exited before it returns.
      return ui.AppExitResponse.exit;
  }
}