requestPermission method

Future<bool> requestPermission(
  1. KruzrPermission kruzrPermission
)

Requests a specific permission from the user.

This method displays the system permission dialog to the user and waits for their response. It should be called when the permission is required for app functionality. Includes error handling for failed permission requests.

Parameters:

Returns: A Future<bool> that resolves to:

  • true if the user granted the permission
  • false if the user denied the permission

Throws:

  • Exception with message "Unable to request permission" if the underlying permission request fails

Note:

  • On some platforms, if a permission was previously denied with "don't ask again", this method may return false immediately without showing a dialog.
  • Consider using ensurePermissionOrOpenSettings for a more comprehensive permission handling flow.

Example:

try {
  bool granted = await requestPermission(KruzrPermission.camera);
  if (granted) {
    // Permission granted, can use camera features
  } else {
    // Permission denied, show alternative or explanation
  }
} catch (e) {
  // Handle permission request error
  print('Failed to request permission: $e');
}

Implementation

Future<bool> requestPermission(KruzrPermission kruzrPermission) async {
  try {
    return await kruzr_comm.requestPermission(kruzrPermission);
  } on Exception catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in requestPermission");
      print(stackTrace);
      print(e);
    }
    return Future.error("Unable to request permission");
  }
}