Flutter Package That Makes Handling Permissions Easy - permission_handler #Flutter #AppDevelopment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial i'm going to show you how to use the flutter package permission handler this is the simple app we're going to build today where we simply have three buttons the first button is to request for example the camera permission when we click on it then we get a pop-up which asks us for the permission for the camera then we can either say okay or don't allow also we gonna look at how we can request multiple permissions at the same time and we're also going to open up the app settings if you press on this as you can see here and let's go back so this is the simple app you're going to build today let's get started as mentioned we're going to use the flutter package permission handler this is the current version 9.2.0 all you have to do is click on this here to copy it to your clipboard then go to your pubspec.yaml file and paste it in here and run pubget for the rest of the setup we have to do a couple of things both for android and for ios so let's do it first for android so under your android folder first open up the gradle.properties as you can see here on the android gradle.properties and just make sure that you have these two lines within your properties file then open up your local.properties still under your android folder and then write this in here the flutter.compile sdk version equals to 31. then under android app open up your build.gradle file and here we're going to reference the compile sdk version which is in the end right here and then simply comment out this line and then paste in this line here compile sdk version then we're getting the property from our local properties and then make sure this is the same variable name as you defined in the local.properties file then the last thing we still have to do for android is defining which permissions we want to use within our app so for this go under android app source main and then the androidmanifest.xml and in here you have to define underneath your manifest tag the permissions you want to use so this internet permission is not necessary for this setup but if you want to use anything related to the internet want to access something over the internet within your app then this permission is always useful so what we're going to use today is the camera the storage so if you want to follow along just copy this and paste it into your app also we're going to use the location permission but otherwise i won't be showing you the other ones because it's almost always the same thing you can find a link to the code in the description so if you want to use other permissions you can simply go to the android manifest within the code and just copy and paste it into your android manifest that's it for the setup for android now for ios go under your ios folder and then open up the port file if you don't see a pod file yet that means you haven't run your ios app yet that's no problem just copy this whole file from the code i provided in the description and just paste it into your ios folder and here underneath the post install within these loops here the target.buildconfiguration.each here you define your permissions you want to use note that a hashtag in front of a line means this is a comment and will be ignored by the compiler so if you wanted to use the camera and if this is commented then you should uncomment this you could also disable this permission also by saying equals to zero otherwise if you want to use it equal to one you don't need these other permissions if you don't want to use them i just put all of them in there so that you have an idea of which permissions you can use then once you know which permissions you want to use and you've defined them in your pod file then go to your ios folder again then under runner open up your info.plist file and then at the end of the info.plist file still within the dictionary tag here define which kind of permissions you want to use for example these are all the permissions we can use for location services and also for the camera so again if there are other permissions you want to use and i'm not discussing them they are in here so just check out the code in the description and then simply do almost the same thing as me just use a different permission this is all we need to do for the setup now let's get started with our app itself for this i simply created a very basic app and concerning our layout i simply made a column where we have three elevated buttons the first one is for requesting camera permission second one is for requesting multiple permissions and the third one is for opening up the permission settings so let's first discuss the request camera permission so first of all what we can do is simply using our permission library so to do this simply import the permission library up here and then we can check okay permission dot camera here you have access to all the other permissions you want to use so for example if you wanted to use bluetooth or contacts it's the same way you handle this so in case for example the camera we then say permission.camera.status and then the status can either be granted denied restricted or permanently denied so here we assign the status to our variable and then this variable we can check is granted if so then we can simply print out is granted or whatever you want to do and then open up the camera for example otherwise if it's denied then we can use permission.camera.request this will then open up a popup window which asks you for the permission for the camera and in case this is granted then you can again open up the camera for example so since i already clicked on this and this will then print out simply in our log that the permission is granted since it's already granted since i granted this when i showed you this in the beginning of the tutorial but again here you can also check for other types of statuses so now if you want to request multiple permissions for example in this case i'm asking for the location permission and the storage permission so all you have to do is define a map with permission and permission status and then say wait permission.location permission.storage and then request those and then when i press on this it's going to open up the popup for example for the permission here and we're going to say allow once for example and as you can see here i'm printing it out so location permission granted and the storage permission is also granted now for the last button for opening up the permission settings so for example if the speech permission is permanently denied what you can do is check if this is permanently denied then you can just call this function this function is directly from the permission handler package and so when you press on this this will simply open up the settings app with our custom app i hope you like this tutorial please leave a like and subscribe if you have any questions feel free to ask them down below in the comments thank you for watching
Info
Channel: eclectify University - Flutter
Views: 10,167
Rating: undefined out of 5
Keywords: permission_handler, Permission Handler, permissions_handler
Id: mEYcU5GtTUs
Channel Id: undefined
Length: 6min 39sec (399 seconds)
Published: Mon Apr 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.