Permission Handler Usage and Implementation – Flutter

Permission handler :

In this blog we will be covering the permission handler concept in flutter app and thereafter we will fetch user permission for accessing device location, camera so on.

In our previous blog permission handling we have discussed how we can fetch the permission from user and in this blog we will be going through the most updated way of dealing with permissions in flutter.

As we are already aware the importance of permissions in apps now a days to reduce the spam issue.We have clearly discussed the importance in this blog may refer.

 

Permission handler Video Tutorial :

Go through the below video tutorial for detailed implementation of permissions in flutter app.

pubspec.yaml :

Let’s us add permission handler dependency with latest version.

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  permission_handler: ^10.2.0

 

manifest :

Adding few permissions in manifest file for demo purpose.As this is a mandatory step in app development don’t skip this part.

The permissions specified here can only be requested through flutter app.

<!-- Required only if your app needs to access images or photos
 that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<!-- Required only if your app needs to access videos
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Required only if your app needs to access audio files
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 

main.dart :

In this file we are going to deal with the permission handling and i have added a basic button to check and request for permission if not granted.

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text("Permission handler"),),
        body: Home(),
      ),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(child: TextButton(onPressed: (){
      checkPermission(Permission., context);
    }, child: const Text("Check Permission")));
  }

  Future<void> checkPermission(Permission permission, BuildContext context) async{
    final status = await permission.request();
    if(status.isGranted){
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Permission is Granted")));
    }else{
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Permission is not Granted")));

    }
  }
}

 

Podfile

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
      # You can remove unused permissions here
      # for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
      # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
        ## dart: PermissionGroup.calendar
        'PERMISSION_EVENTS=1',
        ## dart: PermissionGroup.reminders
        'PERMISSION_REMINDERS=1',
        ## dart: PermissionGroup.contacts
        'PERMISSION_CONTACTS=1',
        ## dart: PermissionGroup.camera
        'PERMISSION_CAMERA=1',
        ## dart: PermissionGroup.microphone
        'PERMISSION_MICROPHONE=1',
        ## dart: PermissionGroup.speech
        'PERMISSION_SPEECH_RECOGNIZER=1',
        ## dart: PermissionGroup.photos
        'PERMISSION_PHOTOS=1',
        ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
        'PERMISSION_LOCATION=1',
        ## dart: PermissionGroup.notification
        'PERMISSION_NOTIFICATIONS=1',
        ## dart: PermissionGroup.mediaLibrary
        'PERMISSION_MEDIA_LIBRARY=1',
        ## dart: PermissionGroup.sensors
        'PERMISSION_SENSORS=1',
        ## dart: PermissionGroup.bluetooth
        'PERMISSION_BLUETOOTH=1',
        ## dart: PermissionGroup.appTrackingTransparency
        'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
        ## dart: PermissionGroup.criticalAlerts
        'PERMISSION_CRITICAL_ALERTS=1',
      ]
    end
  end
end

info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>App needs access to photo lib for profile images</string>
<key>NSCameraUsageDescription</key>
<string>To capture profile photo please grant camera access</string>

 

If you have any query’s in this tutorial do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

Show Buttons
Hide Buttons
Read previous post:
create pdf
Create pdf file in your app | Flutter

Create PDF : Create PDF is a feature which can be used to create a documents based on the requirement...

Close