Handling Permission in Flutter Apps | Permission Handlers | The right way to check for permissions

vijaycreations
29 Apr 202309:14

Summary

TLDRThis FL tutorial explains how to handle permissions in a Flutter app using the 'permission_handler' package. The video demonstrates how to request and manage permissions for accessing device-specific APIs, such as the camera or gallery. It covers how to track permission status, display alerts, and guide users to system settings to modify denied permissions. The tutorial walks through implementing this feature, handling exceptions, and ensuring a smooth user experience by rendering the appropriate UI components based on permission status.

Takeaways

  • 📱 The tutorial focuses on handling permissions in a Flutter app, specifically for accessing device-specific APIs like the camera, microphone, and gallery.
  • 🔑 The 'permission_handler' package is introduced as a tool to manage permission requests and track their status across Android and iOS platforms.
  • 📸 An example is provided where the app uses the 'permission_handler' to access the device's gallery, with an elevated button to trigger the permission request.
  • 🚫 If the user denies the permission request, the app uses the 'permission_handler' to check the status and displays an alert dialog to educate the user about the denied permission.
  • 🔄 The tutorial demonstrates how to handle exceptions that occur when a permission is denied, using a try-catch block to manage the flow of the app.
  • 🛠️ The 'image_picker' package is used in conjunction with 'permission_handler' to open the device gallery and select a photo.
  • 📝 The tutorial guides through adding dependencies in the 'pubspec.yaml' file, which are essential for image picking and permission handling.
  • 🔔 It emphasizes the importance of informing users when a permission is denied and providing options to change the permission settings manually.
  • 🔄 The 'open_app_settings' method from the 'permission_handler' package is highlighted as a way to direct users to the app's settings page to modify permissions.
  • 💻 The tutorial concludes with a coding segment that implements the discussed features, enhancing the app's user interface based on permission status.
  • 🎥 The video script is designed to help developers create more intuitive apps by effectively managing permissions and providing a better user experience.

Q & A

  • What is the purpose of the 'permission_handler' package in Flutter?

    -The 'permission_handler' package is used to manage device-specific permissions, such as access to the camera, microphone, or gallery. It raises permission requests and tracks their status, allowing developers to control app behavior based on the user's response.

  • How does the 'permission_handler' package help in handling denied permissions?

    -When a permission is denied, the 'permission_handler' package keeps track of the permission status and can notify the user. It allows the app to show a dialog informing the user that the permission was denied and gives an option to open system settings to manually change the permission.

  • What happens when a user denies a permission in a Flutter app using 'permission_handler'?

    -If a user denies a permission, the app won't be able to access the requested device feature (like gallery or camera). The permission status is tracked, and the app can inform the user, providing an option to navigate to the system settings to change the permission manually.

  • What is the difference in app behavior when a permission is granted versus when it is denied?

    -When permission is granted, the app follows a sequential flow, allowing access to the requested feature, such as opening the gallery. If permission is denied, the app will handle the exception by showing a dialog with options to open settings and change the permission.

  • How can developers prevent platform exceptions when a permission is denied?

    -Developers can prevent platform exceptions by wrapping the permission request logic inside a try-catch block. This ensures that any permission denial is caught and handled gracefully without crashing the app.

  • What is the purpose of the 'Image Picker' package in the example?

    -The 'Image Picker' package is used to open the device gallery and allow users to choose an image. It works in conjunction with the 'permission_handler' package to request access to the gallery and handle permissions.

  • How does the app notify the user when a permission is denied?

    -When a permission is denied, the app uses an alert dialog to notify the user. The dialog includes information about the denied permission and offers options such as 'Cancel' or 'Settings' to allow the user to navigate to system settings and change the permission.

  • What role does the 'openAppSettings' method play in the permission flow?

    -The 'openAppSettings' method allows the app to directly open the system settings, enabling the user to manually change the permission that was previously denied. This is helpful when the app needs access to a specific device feature.

  • What should developers add to the 'pubspec.yaml' file to implement the features discussed in the video?

    -Developers should add the 'permission_handler' and 'image_picker' packages as dependencies in the 'pubspec.yaml' file to implement permission handling and image selection from the gallery.

  • How does the app behave after a denied permission is manually granted through system settings?

    -Once the denied permission is manually granted through system settings, the app behaves as if the permission was granted initially. For instance, if the gallery permission was granted, the app will directly open the gallery the next time the user clicks the corresponding button.

Outlines

00:00

📱 Introduction to Handling Permissions in a Flutter App

The video introduces handling permissions in a Flutter app using the 'Permission Handler' package. This package allows the app to request access to device-specific APIs like the camera, microphone, or gallery, and helps track the permission status across platforms (iOS and Android). The tutorial demonstrates a simple use case where a button triggers the gallery access, raising a permission request. If the request is denied, the user is informed through the package's built-in status tracking, which also enables them to revoke or modify permissions through system settings.

05:01

🚫 Managing Denied Permissions and Handling Exceptions

The second part explains how to handle denied permissions and related exceptions using try-catch blocks. If the user denies the permission, the app throws a platform exception, which is caught and handled gracefully. By using the Permission Handler package, the app tracks permission status, showing an alert dialog to notify the user if a permission is denied. The user can then navigate to system settings to change the permissions manually. The flow is demonstrated for gallery access but can also be applied to other device-specific APIs like the camera or microphone.

Mindmap

Keywords

💡Permissions

Permissions refer to the access rights that an app requests from a user to utilize certain device features, such as the camera, microphone, or gallery. In the video, handling permissions is a key topic, where the app asks for permission to access the gallery. If the permission is denied, the app reacts accordingly, displaying a dialogue or guiding the user to system settings.

💡Permission Handler

Permission Handler is a package used in Flutter apps to manage and track the status of permissions. It allows developers to check if permissions have been granted or denied for accessing device-specific APIs like the gallery or camera. The script demonstrates how to use this package to request permissions and handle cases when permissions are denied or revoked.

💡Device-specific APIs

Device-specific APIs refer to functions that allow apps to interact with hardware features like the camera, microphone, or gallery. The video focuses on accessing the gallery through device-specific APIs. The app needs to request permission to use these APIs, which is where permission handling becomes important.

💡Image Picker

Image Picker is a Flutter package that provides an easy way to access and select images from the device's gallery. In the video, Image Picker is used to allow the user to pick a photo from the gallery. It is integrated with the Permission Handler to check whether the app has permission to access the gallery.

💡Exception Handling

Exception Handling refers to managing errors or unexpected situations that arise during code execution. In the video, exception handling is used to capture errors when the app tries to access the gallery without permission. The developer wraps the code in a try-catch block to handle the exception and inform the user about the denied permission.

💡Alert Dialog

An Alert Dialog is a pop-up message that notifies or prompts the user for action. In the video, an alert dialog is shown to inform the user when gallery access permission is denied. The dialog offers options like 'Cancel' or 'Open Settings' to guide the user in managing app permissions.

💡Settings Button

The Settings Button is an action provided within the alert dialog that directs users to their device’s settings, where they can manually change app permissions. The video shows how the button is used in situations where the user has denied permission, helping them navigate to system settings to grant or revoke permissions.

💡Elevated Button

An Elevated Button is a common Flutter widget that represents a clickable button with an elevation effect. In the video, an elevated button is used in the app interface to trigger the process of opening the device gallery, which in turn requests permission if it hasn’t been granted already.

💡UI Components

UI Components refer to elements that make up the user interface of an app, such as buttons, dialogs, and bars. In the video, custom UI components like the 'App Bar' and 'Primary Button' are used to create a functional and interactive app layout. The permission handling also ties into UI by altering the interface based on permission statuses.

💡Cross-platform API

A Cross-platform API allows developers to write code that works across multiple operating systems, such as Android and iOS, without needing platform-specific implementations. In the video, the Permission Handler provides a cross-platform solution for managing permissions, allowing the app to function similarly on both Android and iOS devices.

Highlights

Introduction to handling permissions in a Flutter app, focusing on accessing device-specific APIs like camera, microphone, and gallery photos.

Introduction of the 'permission_handler' package in Flutter to request, track, and manage permissions across Android and iOS platforms.

Using the 'permission_handler' package to handle permission requests when accessing the device gallery, either granting or denying access.

Description of the process for granting or denying permissions for device-specific APIs, such as the gallery, and how the app reacts to each.

Explanation of how the app can keep track of permission status and provide users with information about denied permissions through alerts.

Illustration of how the 'permission_handler' package allows the user to revoke or change permissions manually through system settings.

Walkthrough of creating the user interface with a custom app bar and a primary button that triggers the permission request.

Implementation of the 'getFromGallery' method using the 'image_picker' package to open the device gallery and select a photo.

Detailed explanation of wrapping permission logic inside a try-catch block to handle exceptions and display error messages if permissions are denied.

The app ensures that once permission is denied, the user will not be asked again to grant the permission, making it a one-time process.

Handling denied permissions through an alert dialog that offers users options to either cancel or navigate to system settings to modify permissions.

Introduction of the 'openAppSettings' method from the 'permission_handler' package to directly guide users to the app’s settings page.

Explaining how the app checks permission status, educates users about permission denial, and updates the UI accordingly.

Demonstration of how denied permissions can be re-enabled through system settings, allowing the app to access the requested API again.

Clarification that the same permission handling process applies to other device-specific APIs like the camera and microphone, making the app more intuitive for users.

Transcripts

play00:02

[Music]

play00:06

hey everyone welcome back again to

play00:07

another FL tutorial and in this s we

play00:09

will look into how to handle permissions

play00:11

in our f app so the permission here what

play00:14

we talk about is the ability to access

play00:15

the device specific apas like the camera

play00:18

microphone or the gallery photos Etc and

play00:21

to make this possible we'll be using one

play00:24

such package available in pb. called the

play00:26

permission handless and you can also

play00:28

take a look at that and this is going to

play00:30

be the package which we have been

play00:32

talking right now the permission

play00:33

handless which actually does the job of

play00:35

raising the request whenever the flat

play00:38

appap invokes any device specific AP

play00:40

access like the camera microphone and

play00:42

also at the same time this package keeps

play00:44

track of the permission status and you

play00:46

can also see that this package provides

play00:48

a close platform Android and iOS API to

play00:51

request and check for the permissions so

play00:54

this package actually comes in handy

play00:56

while you're dealing with uh the process

play00:58

of accessing the device specific AP in

play01:00

your frap now let's head back to our vs

play01:02

code and try to understand this with the

play01:04

help of a simple example what we have

play01:06

here is a running example of the same we

play01:08

have used the permission Handler package

play01:11

to access the device Gallery we have a

play01:13

elevated button placed right to the

play01:14

center and which when click it should

play01:16

take us to the device gallery and allow

play01:18

us to choose a particular photo consider

play01:20

we have opened the app for the very

play01:22

first time and if I try to click this

play01:24

button it rises the request whether to

play01:27

approve or deny the request for the

play01:28

particular AP which which is the photo

play01:30

in this case you can either go for

play01:33

approving the request or you can also

play01:35

deny the request if you go for approving

play01:36

the request the process is going to be

play01:38

linear nothing is going to change rather

play01:40

consider if you try to deny the request

play01:42

where we click this don't allow button

play01:44

now we have deny the request either

play01:45

knowingly unknowingly and if we try to

play01:47

click this button again we will no

play01:49

longer see the request to revoke the

play01:51

permission or to change the permissions

play01:53

rather with the help of the permission

play01:54

handers you'll be able to keep track

play01:56

with the status and since we are using

play01:58

this package in a f app it try to

play02:00

provide the user with the relevant

play02:01

information of the permission status

play02:03

that the permission has been denied for

play02:05

the photo or the gallery and this is

play02:07

done possible with the help of the

play02:09

package permission handers just with the

play02:11

help of the status we'll be able to

play02:13

enter this alert box and educate the

play02:15

user about the permission status where

play02:17

the user can go directly to the system

play02:22

settings and revoke the permission or

play02:24

change the permission manually in this

play02:26

case the permission is being denied

play02:29

let's change it to all photos and head

play02:31

back to our FL appap and this time if I

play02:34

try to click this button you will no

play02:36

longer see that alert box rather it

play02:38

takes us directly to the device gallery

play02:40

and allow us to choose the photo since

play02:42

the permission has been granted so this

play02:44

is where the usage of the package comes

play02:46

in it keeps track of the permission

play02:47

status either it is approved or denied

play02:49

based upon which we can render the UI

play02:52

accordingly either to ask for a

play02:54

permission again or to go with the flow

play02:56

of the app itself hope you got a better

play02:58

understanding of how to make use of the

play02:59

the permission handlers to process the

play03:01

request and keep track of the permission

play03:03

status in a fredra with this idea and

play03:06

without any further delay let's directly

play03:08

jump into the coding part and start

play03:09

implementing this feature in our

play03:14

flra first let's try to add two

play03:16

dependencies in our perspect and the

play03:18

dependencies are the image picker and

play03:22

the permission Handler the image Pier is

play03:24

the package that helps us to open the

play03:26

device gallery and choose the photo and

play03:28

the permission Handler is what we have

play03:30

been talking earlier all right so make

play03:32

sure you add these two dependencies in

play03:34

your perspect and after adding these two

play03:36

dependencies let's head over to the

play03:38

main.

play03:40

file here in the main. file the home

play03:43

points to my homepage the my homepage is

play03:45

nothing but a stateless wiget class with

play03:47

the empty scaold first let's start by

play03:49

building up the uis let's create AB bar

play03:53

right so this abar is nothing but a

play03:55

custom defined function which is written

play03:57

down separately inside the components

play03:59

folder here inside the components we

play04:01

have the app bar defined which is going

play04:02

to be a generic app bar so we just need

play04:05

to pass the title and few other optional

play04:07

parameters and the rendering will be

play04:09

done accordingly so we'll make use of

play04:11

this build AB bar wiet which is a user

play04:14

defined

play04:15

function and after the app bar let's

play04:17

start with the body where inside the

play04:19

body we have a primary button which is

play04:22

an elevated button technically just the

play04:24

same way this primary button is nothing

play04:26

but a custom defined function that

play04:28

internally renders the elevated button

play04:30

widget okay so we make use of this

play04:32

primary button which is written down

play04:34

inside the same components folder just

play04:36

like the app bar

play04:38

okay and the button function is going to

play04:40

be the get from gallery that we will

play04:42

Define shortly second parameter is going

play04:43

to be the button text now let's try to

play04:45

Define this get from Gallery

play04:48

method here this get from Gallery method

play04:50

is where we will be writing the logic

play04:52

for opening the device gallery and try

play04:54

to pick a photo so let's make use of the

play04:57

image picker and make use of the method

play04:58

pick image

play05:00

now we have written down the code for

play05:02

picking the image from the gallery we

play05:04

haven't made use of the permission

play05:05

Handler here so let's try running this

play05:07

app and see how it

play05:09

works since I am opening the app for the

play05:12

first time it asks for allowing or

play05:13

denying the request if I click allow the

play05:15

flow is going to be sequential no ex is

play05:17

going to happen but consider I click

play05:20

this don't allow that is I deny the

play05:22

request you see that a platform

play05:24

exception has occurred in order to

play05:26

handle this exception we need to wrap

play05:28

this code inside the catch

play05:32

block and inside the catch block let's

play05:34

try to just print the exception and if I

play05:38

refresh this app you no longer get the

play05:40

permission request again it is a one

play05:41

time process so

play05:44

let's delete this

play05:46

app and try to reun the entire project

play05:49

once

play05:50

again here the only change is that we

play05:52

have wrapped this code inside the TR

play05:54

cast block and if I click this and try

play05:56

to deny the request you will no longer

play05:59

see the except extion rather you will be

play06:00

able to see the exception here in the

play06:03

debug

play06:04

console this is one decent way of

play06:06

handling the exceptions while dealing

play06:08

with the permissions but this is not

play06:10

something which we are about to discuss

play06:11

here so so let's get of that and here

play06:15

inside the cat block which means that

play06:17

the permission has been denied so this

play06:18

catch block get executed only if there

play06:20

is an exception happening here during

play06:22

the process of granting or denying the

play06:24

request considering the possibility of

play06:26

denying the request this catch block

play06:28

gets executed right therefore inside

play06:30

this cat block let's make use of the

play06:32

permission status with help of the

play06:34

permission Handler we'll be able to

play06:36

check for the status of the permission

play06:38

here in this case we use the photos you

play06:40

can also check for the

play06:42

camera or the

play06:44

microphone EXC thanks to the permisson

play06:46

Handler package that helps us keep track

play06:48

of the status and based upon the status

play06:51

if it is denied if the status is denied

play06:53

we try to show alert dialogue or else we

play06:57

try simply try to print the exception

play06:58

here and let's now try to Define this so

play07:00

aler

play07:02

dialogue this so alert dialog is going

play07:04

to be a Cino dialogue and inside which

play07:06

we try to define the title as permission

play07:08

denied since the status is denied we try

play07:11

to provide the user with the relevant

play07:12

information of the same and we try to

play07:14

have two Cino action dialogue one is

play07:16

going to be the cancel button and second

play07:18

is going to be the settings button which

play07:20

will in turn take us to the system

play07:22

settings to do the process of changing

play07:24

the permission manually whenever the

play07:25

cancel buttons press we are not going to

play07:27

do anything just we will try to close

play07:28

the alert dialog here inside the

play07:30

settings whenever the button is pressed

play07:31

we try to make use of the open app

play07:33

settings method which we get as a result

play07:35

of the permisson Handler package okay so

play07:38

this open app settings will take us

play07:40

directly to the system settings and do

play07:42

the process of reworking or either

play07:44

changing the permission based upon the

play07:45

user choice so let's refresh this

play07:48

app so consider that we have dened the

play07:50

request for the first time and try to

play07:51

click this button the second time we try

play07:53

to get this s dialog based upon the

play07:56

status since there is an exception

play07:57

occurring here as the has been denied

play08:00

for opening the device Gallery this cat

play08:02

block get executed and here inside the

play08:04

cat block as the permission is denied we

play08:07

try to S the alert dialog this alert

play08:08

dialog is going to have two options

play08:10

either to cancel or open the system

play08:12

settings to do the manually let's go to

play08:14

the system settings that will take us

play08:16

directly to the particular flat app

play08:18

setting and here you see that since we

play08:20

have denied the request we have no

play08:22

access to any of the photos let's click

play08:24

this and change it to all photos and now

play08:26

let's move back to our f app and let's

play08:28

try to click this button again this time

play08:32

we will no longer see the alert dialogue

play08:34

as the permission has been granted so

play08:36

this is how it works the same way for

play08:38

the camera microphone and all of the

play08:39

device specific apis therefore with the

play08:41

help of this permission handlers be able

play08:43

to keep track of the request and also

play08:45

educate the user about the Peron

play08:47

statuses and render the UI components

play08:50

accordingly thereby making your app more

play08:52

intuitive for the users hope you guys

play08:53

found this tutorial useful if you do so

play08:55

consider subscribing and I will see you

play08:57

in the next video

play08:59

a

play09:00

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
App PermissionsDevice AccessFlutter TutorialPermission HandlersImage PickerUser InterfaceMobile DevelopmentApp SettingsPrivacy ControlFlutter App