didRequestAppExit method

  1. @override
  2. @protected
  3. @mustCallSuper
Future<AppExitResponse> didRequestAppExit()
inherited

Called when a request is received from the system to exit the application.

Implementation

@override
@protected
@mustCallSuper
Future<AppExitResponse> didRequestAppExit() async {
  /// Exiting the application can proceed.
  var appResponse = AppExitResponse.exit;

  // A triggered system event
  _hadSystemEvent = true;

  // Don't if the State object is defunct.
  if (!mounted) {
    return appResponse;
  }

  /// No 'setState()' functions are allowed to fully function at this point.
  _setStateAllowed = false;

  // All must allow an exit or it's cancelled.
  for (final con in controllerList) {
    //
    final response = await con.didRequestAppExit();

    /// Cancel and do not exit the application.
    if (response == AppExitResponse.cancel) {
      // Record the 'cancel' response
      appResponse = response;
    }
  }

  _setStateAllowed = true;

  // If staying with the app and a setState() was requested.
  if (_setStateRequested && appResponse == AppExitResponse.cancel) {
    _setStateRequested = false;
    // Only the latest State is rebuilt
    if (isEndState) {
      /// Perform a 'rebuild' if requested.
      setState(() {});
    }
  }

  return appResponse;
}