Permission_handler package

The permission_handler package is a Flutter plugin that provides a cross-platform API to request and check the status of various types of permissions on both Android and iOS devices. The package supports requesting and checking the status of permissions related to location, camera, microphone, storage, contacts, and more.

In this article, we’ll explore how to use the permission_handler package to request and check the status of various types of permissions in Flutter.

Getting started

To use the permission_handler package in your Flutter app, you need to add it to your pubspec.yaml file:

dependencies:
  permission_handler: ^8.1.2

After adding the dependency, you need to run flutter pub get to download the package.

Requesting permissions

To request a permission using the permission_handler package, you need to first check the current status of the permission using the Permission.status method. Once you know the current status of the permission, you can request it using the Permission.request method.

Here’s an example of how to request the Location permission:

import 'package:permission_handler/permission_handler.dart';

Future<void> requestLocationPermission() async {
  if (await Permission.location.request().isGranted) {
    // Permission is granted
  } else {
    // Permission is not granted
  }
}

In this example, we first check the status of the Location permission using the Permission.status method. If the status is granted, we do nothing. If the status is not granted, we request the permission using the Permission.request method.

You can replace Permission.location with any other type of permission you want to request.

Checking permissions

To check the current status of a permission using the permission_handler package, you can use the Permission.status method. The method returns a Future<PermissionStatus> object that you can use to check the status of the permission.

Here’s an example of how to check the status of the Location permission:

import 'package:permission_handler/permission_handler.dart';

Future<PermissionStatus> getLocationPermissionStatus() async {
  final status = await Permission.location.status;
  return status;
}

In this example, we use the Permission.location.status method to get the current status of the Location permission. The method returns a Future<PermissionStatus> object that we can use to check the status of the permission.

You can replace Permission.location with any other type of permission you want to check.

Handling permission status changes

If the user grants or denies a permission, you can listen to permission status changes using the Permission.status stream. The stream emits a new PermissionStatus object every time the status of a permission changes.

Here’s an example of how to listen to permission status changes for the Location permission:

import 'package:permission_handler/permission_handler.dart';

void listenForLocationPermissionStatusChanges() {
  Permission.location.status.listen((status) {
    // Handle permission status change
  });
}

In this example, we listen to permission status changes for the Location permission using the Permission.location.status stream. Every time the status of the Location permission changes, we handle the status change by calling a function that you can replace with your own implementation.

You can replace Permission.location with any other type of permission you want to listen to.