How to handle permission requests in Flutter

--

How to handle permission requests in Flutter — In this lesson, we are going to look at how to request and check permissions on Android and IOS in Flutter.

Package(s)

Installation

The process of installing a flutter package is quite simple, just open the pubspec.yaml file and add the package into your dependency bloc section.

dependencies:
permission_handler:

TIP: You can use caret versioning to restrict the package version to a specific major version. For instance, permission_handler: ^11.0.0 restricts it to version 11, but you will get both minor updates and patches.

Permissions

First, we need to determine which permissions we need, because your app needs to publicly declare the permissions it needs. Some less-sensitive permissions such as the internet, etc. are granted automatically. While other more sensitive permissions, think of Location, Contacts, etc. require user authorisation before your app can use them.

iOS Permissions

In iOS, this done by adding the permissions to Information Property List File i.e. info.plist, with the Permission you need being the Key, accompanied by the reason why your app needs to use the feature.

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires to save your images user gallery</string>

In Flutters’ case, the info.plist in the iOS/Runner directory at the root of your project. You can learn more about the info.plist here.

You can find the complete list of permissions here and guidelines for asking permission on iOS here.

Android Permissions

In Android, you can achieve the same by adding the <uses-permission> tag to the android manifest, this in the android/src/main/AndroidManifest.xml directory.

<manifest ...>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application ...>
...
</application>
</manifest>

You can learn more about permission on Android here and the best practices here.

Requesting for Permission

To request permission, first you need to import the package:

import 'package:permission_handler/permission_handler.dart';

And then, let say you want to request contact permission, you can do it like this. You pass a list of permissions you are requesting for, which allows you to ask for multiple permissions you need at once.

How to use

There are a number of Permissions. You can get a Permission's status, which is either granted, denied, restricted, permanentlyDenied, limited, or provisional.

var status = await Permission.camera.status;
if (status.isDenied) {
// We didn't ask for permission yet or the permission has been denied before, but not permanently.
}

// You can can also directly ask the permission about its status.
if (await Permission.location.isRestricted) {
// The OS restricts access, for example because of parental controls.
}

Call request() on a Permission to request it. If it has already been granted before, nothing happens.
request() returns the new status of the Permission.

if (await Permission.contacts.request().isGranted) {
// Either the permission was already granted before or the user just granted it.
}
// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
Permission.location,
Permission.storage,
].request();
print(statuses[Permission.location]);

Some permissions, for example location or acceleration sensor permissions, have an associated service, which can be enabled or disabled.

if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
// Use location.
}

You can also open the app settings:

if (await Permission.speech.isPermanentlyDenied) {
// The user opted to never again see the permission request dialog for this
// app. The only way to change the permission's status now is to let the
// user manually enable it in the system settings.
openAppSettings();
}

On Android, you can show a rationale for using a permission:

bool isShown = await Permission.contacts.shouldShowRequestRationale;

Some permissions will not show a dialog asking the user to allow or deny the requested permission.
This is because the OS setting(s) of the app are being retrieved for the corresponding permission.
The status of the setting will determine whether the permission is granted or denied.

--

--

Prashant Vaddoriya - Senior Flutter Developer

Senior Flutter Developer | Mobile App Development | Android App Development | iOS App Development