Example of Permission Handler Request Single Runtime Permission in Flutter Android

Hello friends, Few days ago when I was reading about Runtime permissions in android for flutter then I thought, Why should not make a tutorial on this topic. So I started reading about this topic further. After searching a few queries on Google, I realized that flutter makes implementing runtime permissions for android is so much easy for us by providing us a Pub Package known as permission_handler. So in today’s tutorial we would learn about Example of Permission Handler Request Runtime Permission in Flutter Android. Today we are only implementing single runtime permission. So we will learn about the source code for requesting GPS location permission access and requesting camera access permission in today’s tutorial.

Contents in this project Example of Permission Handler Request Single Runtime Permission in Flutter Android :-

1. First of all we have to download and install the permission_handler PUB Package in our current flutter project. So open your flutter project Root directory in CMD in windows and in Terminal in MAC and execute below command.

Screenshot :-

Screenshot after complete installation :

2. Now the package is successfully installed in our project, Next step is to add the Permission in our AndroidManifest.xml file. So Goto Your_Flutter_Project -> android -> app -> src -> main -> AndroidManifest.xml. Open the AndroidManifest.xml file in any code editor. I’m using Visual Studio Code editor. Now here comes the main part we’re implementing Access Location Permission and Camera Access Permission. So we have to add both of these permissions in our AndroidManifest.xml file.

Source Code of my AndroidManifest.xml file after adding above permission :

3. Now all the config part has finished. In next step we would start writing code for application. So open your project’s main.dart file and import material.dart and permission_handler.dart package.

4. Creating our main void main runApp() method and here we would call our main App class.

5. Creating our main App extends StatelessWidget class. In this class we would call AppState class.

6. Creating our AppState extends StatefulWidget class. In this class we would make createState() State management method along with AppPermission class to enable mutable state in AppPermission class.

7. Creating our main Child AppPermission extends State<AppState> class. In this class we would do all the main coding for app.

8. Creating a function named as requestCameraPermission() Future type of ASYNC. In this function we would request runtime camera permission for android.

9. Creating another function named as requestLocationPermission() Future type of ASYNC. In this function we would ask for Location permission on app runtime.

10. Creating Widget Build area then we would make 2 ElevatedButton widget and call above both requestLocationPermission() and requestCameraPermission() function on button onPress event.

11. Complete source code for main.dart file :-

Screenshots :-

Leave a Reply

Your email address will not be published.