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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] so the permission here what we talk about is the ability to access the device specific a base like the camera microphone or the gallery photos Etc and to make this possible we will be using on such package available in pub.dev called the permission headless and you can also take a look at that and this is going to be the package which we have been talking right now the permission handles which actually does the job of pricing the request whenever the flat wrap invokes any device specific AP access like the camera microphone and also the same time this package keeps track of the permission status and you can also see that this package provides a close platform Android and iOS AP to request and check for the permissions so this package actually comes in handy while you're dealing with the process of accessing the device specific air base in your flat track now let's head back to our vs code and try to understand this with the help of a simple example what we have here is a running example of the same we have used the permission Handler package to access the device Gallery we have a elevated button placed right to the center and which one click it should take us to the device gallery and allow us to choose a particular photo consider we have opened the app for the very first time and if I try to click this button it rises the request whether to approve or deny the request for the particular AB which is the photo in this case you can either go for approving the request or you can also deny the request if you go for approving the request the process is going to be linear nothing is going to change rather consider if you try to deny the request where we click this don't allow button now we have neither request either knowingly or knowingly and if we try to click this button again we will no longer see the request to rework the permission or to change the permissions rather with the help of the permission handlers you will be able to keep track of the status and since we are using this package in our fat wrap we try to provide the user with the relevant information of the permission status that the permission has been denied for the photo or the gallery and this is done possible with the help of the package permission handles just with the help of the status we'll be able to enter this alert box and educate the user about the permission status where the user can go directly to the system settings and revoke the permission or change the permission manually in this case the permission is being denied let's change it to all photos and head back to our filter app and this time if I try to click this button you will no longer see that alert box rather it takes us directly to the device gallery and allows to choose the photo since the permission has been granted so this is where the usage of the package comes in it keeps track of the permission status either it is approved or denied based upon which we can re-render the UI accordingly either to ask for permission again or to go with the flow of the app itself hope you got a better understanding of how to make users a permission handlers to process the requests and keep track of the permission status in your Federal app with this idea and without any further delay let's directly jump into the coding part and start implementing this feature in our flutter app first let's try to add to dependencies in our perspect and the dependencies are the image picker and the permission Handler the image picker is the package that helps us to open the device gallery and choose the photo and the permission Handler is what we have been talking earlier right so make sure you add these two dependencies in your perspective and after adding these two dependencies let's head over to the main.file here in the main data file the home binds to my home page the my home page is nothing but a status widget class with the empty scaffold first let's start by building up the uis let's create a app bar right so this Amber is nothing but a custom defined function which is written down separately inside the components folder here inside the components we have the app bar defined which is going to be a general Kappa so we just need to pass the title and few other optional parameters and the rendering will be done accordingly so we'll make use of this build widget which is a user defined function and after the app bar let's start with the body where inside the body we have a primary button which is an elevated button technically just the same way this primary button is nothing but a custom defined function that internally renders the elevated button widget okay so we make use of this primary button which is written down and say the same components folder just like the app bar okay and the button function is going to be the get from gallery that we will Define shortly second parameter is going to be the button text now let's try to Define this gate from Gallery method here this get from Gallery method is where we will be writing the logic for opening the device gallery and try to pick a photo so let's make use of the image bigger and make is the method pick image now we have written down the code for picking the image from the gallery we haven't made use of the permission Handler here so let's try running this app and see how it works since I am opening the app for the first time it asks for allowing or denying the request if I click hello the flow is going to be C console no exception is going to happen but consider I click this don't allow that is I deny the request you see that a platform exception has circled in order to handle this exception we need to grab this code inside the try catch block and say the cash block let's try to just print the exception and if I refresh this app you no longer get the permission request again it is a one-time process so let's delete this app and try to rerun the entire project once again here the only changes that we have wrapped this code inside the dry cache block and if I click this and try to deny the request you will no longer see the exception rather you will be able to see the exception here in the debug console this is one decent way of handling the exceptions while dealing with the permissions but this is not something which you are about to discuss here so so let's get rid of that and here inside the cash block which means that the permission has been denied so this catch block get executed only if there is an exception happening here during the course of granting ordaining the request considering the possibility of denying the request the sketch block gets executed right therefore inside this cash block let's make use of the perms and status with help of the permission Handler we will be able to check for the status of the permission here in this case we use the photos you can also check for the camera or the microphone Etc thanks to the permission Handler package that helps us keep track of the status and based upon the status if it is denied if the status is denied we try to alert dialogue or else we try simply try to print the exception here and let us now try to Define this so alert dialog this is all I dialogue is going to be a Cupertino dialogue and inside which we try to define the title as permission denied since the status is denied we try to provide the user with the relevant information of the scene and we try to have two Cupertino action dialogue one is going to be the cancel button and the second is going to be the settings button which will in turn take us to the system settings to do the process of change the permission manually whenever the cancer button is pressed we are not going to do anything just we will try to close the alert dialogue here inside the settings one of the button is pressed it try to make use of the open app settings method which we get as a result of the permission Handler package okay so this open app settings will take us directly to the system settings and do the process of reworking or either changing the permission based upon the user choice so let's refresh this app so consider that we have denied the request for the first time and try to click this button the second time we try to get this so dialog based upon the status since there is an exception occurring here as the function has been denied for opening the device Gallery this catch block get executed and here inside the cache block as the permission is denied we try to show the alert dialogue this alert dialog is going to have two options either to cancel or open the system settings to do the manual English go to the system settings that will take us directly to the particular filler trap setting and here you see that since we have denied the request we have no access to any of the photos let's click this and change it to all photos and now let's move back to our filter app and let's try to click this button again this time we will no longer see the alert dialog as the person has been granted so this is how it works the same way for the camera microphone and all other device specific apis therefore with the help of this permission handlers be able to keep track of the request and also educate the user about the permission statuses and enter the UI components accordingly thereby making your app more intuitive for the users hope you guys found this really useful if you do so consider subscribing and I will see you in the next video [Music] foreign
Info
Channel: vijaycreations
Views: 9,979
Rating: undefined out of 5
Keywords: flutter, permission, permission handlers, check for permission, device permission, flutter device permission, permissions, handling permissions in flutter, permission handler package in flutter, flutter permissions, app, uiux, dev, package, flutter package, permission in flutter apps, device permission in flutter, permission in flutter, permission handler package, permission handler, app dev, flutter tutorial, tutorial, example, android, ios, kotlin, code, mobile, developer, permission status
Id: B2Vjnqlz01E
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Sat Apr 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.