Updated 17 January 2024
Hey guys in this article we’re going to learn about how to Permission Handler in Flutter and monitor permission status in our Flutter apps.
While the permissions are requested during runtime. You’ll still need to tell the Devices which permissions your app might potentially use. That requires adding permission configuration to Android* and iOS-specific files.
We will use this permission_handler package.
Before we’re going to start this blog you may also check our Flutter app development services.
Implementation
In our example, we are going to give access for using the notifications in Android above SDK 31. using a permission handler in flutter and monitor the status of the device permission, whether it is allowed or denied.
With the help of this permission handler
package we will be able to request permission when the flutter app invokes any device-specific APIs access like the Storage, Notification, Camera etc.,
Let’s first create a Text button widget which when clicked will try to access the notification and storage permission.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Notification Permission"), ), body: Center( child: TextButton( onPressed: () { _getFromGallery(context); }, child: const Text("Ask for Storage Permissions"), ), ), ); } _getFromGallery(context) async { try { XFile? imageFile = await ImagePicker().pickImage( source: ImageSource.gallery, maxWidth: 190, maxHeight: 190, ); if (imageFile != null) { File _file = File(imageFile.path); } } catch (e) { print(e.toString()); } } |
In the above code snippet, we haven’t made use of the permission_handler
package rather we simply try to access the device gallery photo. Hence if any exception occurs during the permission request call (if the user deny the request for accessing gallery photo). We will try to catch and display the exception message in the debug console.
Now let’s update the above code snippet (the catch
block code alone) with the help of permission handler package, thereby tracking the status of the permission.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
requestPermission() async { var status = await Permission.storage.status; if (status.isDenied) { await Permission.storage.request(); // access media location needed for android 10/Q await Permission.accessMediaLocation.request(); // manage external storage needed for android 11/R await Permission.manageExternalStorage.request(); Permission.notification.request(); } else if (status.isPermanentlyDenied) { openAppSettings(); } } |
Here the Permission.storage.status
holds the status of the permission (allowed/rejected). Hence if the status is Denied. We will request storage permission or the user change the permission automatically by going into the device settings.
Hence this permission handler
in Flutter becomes too handy to keep track of permission status and update the UI components accordingly thereby making our app more intuitive for the users.
You can also check our other blog here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.