Start a Project

Permission Handler IN Flutter

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.

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.

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.

Conclusion

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.

Exit mobile version