Permission Handling in Flutter with permission_handler

Published on by Flutter News Hub

Permission Handling in Flutter with permission_handler

Managing user permissions is crucial for apps accessing sensitive data or services. The permission_handler plugin simplifies permission handling in Flutter, providing a cross-platform API for requesting permissions and checking their status.

Setup

Before using the plugin, configure permissions in your app's:

  • AndroidManifest.xml (Android): Add permission entries in the main version.
  • Info.plist (iOS): Add permission entries and enable macros in your Podfile.

Code Usage

Get Permission Status

var status = await Permission.camera.status;
if (status.isDenied) { // Permission not granted or denied
}

Request Permission

if (await Permission.contacts.request().isGranted) { // Permission granted
}

Request Multiple Permissions

Map statuses = await [
  Permission.location,
  Permission.storage,
].request();
print(statuses[Permission.location]);

Check Permission Service Status (Android)

if (await Permission.locationWhenInUse.serviceStatus.isEnabled) { // Service enabled
}

Open App Settings

if (await Permission.speech.isPermanentlyDenied) { // Permission permanently denied
  openAppSettings(); // Open app settings to change permission
}

Show Permission Rationale (Android)

bool isShown = await Permission.contacts.shouldShowRequestRationale;

Common Permissions

The plugin supports various permissions, including:

  • Notification
  • Bluetooth
  • Manage External Storage
  • System Alert Window
  • Request Install Packages
  • Access Notification Policy
  • Location Always (follow Android 10+ guidelines)

FAQ

Storage Permissions on Android 13+

Starting with Android 13, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions are deprecated. Use READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO, or MANAGE_EXTERNAL_STORAGE instead.

Permission locationAlways Denied on Android 10+

First request locationWhenInUse permission, then locationAlways.

Permission Checks/Requests Crash on iOS

Ensure all required permissions are listed in Info.plist. For SiriKit access, add the com.apple.developer.siri entitlement.

Flutter News Hub