Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use dynamic permissions in Flutter Development

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use dynamic permissions in Flutter development, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

As we all know, Android changed the permissions to dynamic permissions after version 6.0, while iOS has been using dynamic permissions, so if some dangerous permissions are involved in the development of Flutter applications, you need to apply dynamically, and you can use Flutter's permission_handler to apply for permissions dynamically.

Basic use of 1, configure permissions

First, open the AndroidManifest.xml file under the Android project as follows: configure it in android\ app\ src\ main\ AndroidManifest.xml, and then add the permissions shown below.

Dynamic permission application

At present, the plug-in has been upgraded several versions, and different versions of the approach is not the same, especially the latest version 5.0.0 and previous versions, usage is quite different.

4.3.0

For example, here is the use of version 4.3.0:

Permission list: fields in PermissionGroup

Permission status list: fields in PermissionStatus

Open the permission settings page: await PermissionHandler () .openAppSettings ()

Apply for authority

Examples are as follows:

Await Map map= PermissionHandler () .requestPermissions ([permission list])

Then, there is the status of obtaining the permission to apply, as follows:

PermissionStatus contactsPermStatus = await PermissionHandler () .checkPermissionStatus (PermissionGroup.contacts)

Here is a complete example:

/ / request permission void _ requestPermission () async {debugPrint ("enter the splash screen page"); / / apply permission / / PermissionStatus storageStatus; PermissionStatus cameraStatus; await PermissionHandler () .requestPermissions ([PermissionGroup.camera]) .then ((value) {debugPrint ("return: $value"); / / storageStatus=value [PermissionGroup.storage]; cameraStatus=value [PermissionGroup.camera];}) DebugPrint ("request permission and obtain permission: $cameraStatus"); / / verify permission if (cameraStatus = = PermissionStatus.granted) {debugPrint ("verify permission: all users agree"); / / users all agree (with &) / / all apply for successful initialization of splash screen _ initSplash () } else if (cameraStatus = = PermissionStatus.denied) {debugPrint ("Verification permissions: any set of permissions denied by the user"); / / user refused (with |) / / any set of permissions were denied by the user / / spliced prompt permission text StringBuffer sb = new StringBuffer (); sb.write (cameraStatus = = PermissionStatus.denied? "camera,": "); String tip = Utils.removePostfix (sb.toString (),", ") Utils.showCustomDialog (context, ConfirmDialog ("you have rejected the necessary permissions for the application:\ n [$tip], do you want to reapply?", canBackDismiss: false, confirmCallback: () = > _ requestPermission (), cancelCallback: () = > SystemNavigator.pop (),)) } else if (cameraStatus = = PermissionStatus.neverAskAgain) {debugPrint ("verify permission: permission to deny permanently"); / have permission to permanently deny (with |) / / have any set of permissions to choose not to prompt / / splice prompt permission text StringBuffer sb = new StringBuffer (); sb.write (cameraStatus = = PermissionStatus.neverAskAgain? "camera,": "); String tip = Utils.removePostfix (sb.toString (),", ") Utils.showCustomDialog (context, ConfirmDialog) ("you have disabled the necessary permissions for the application:\ n [$tip], please allow? in the settings", canBackDismiss: false, confirmText: "go to the settings", confirmCallback: () async {await PermissionHandler (). OpenAppSettings (); / / Open the settings page SystemNavigator.pop () }, cancelCallback: () = > SystemNavigator.pop (),);}} 5.0.0

Version 5.0.0 is roughly similar to the previous one, except that the method and parameter fields have changed, as shown below.

Permission list: fields in Permission

Permission status list: fields in PermissionStatus

Open the permission settings page: openAppSettings ()

Apply for authority

Await [permission list] .request (); / / you can use then to obtain status with permissions.

Get permission status await Permission.camera.status

Determine permission status: await Permission.camera.isDenied | isGranted, etc.

The following is a detailed example of usage:

/ / request permission void _ requestPermission () async {debugPrint ("enter the splash screen page"); / / apply permission / / PermissionStatus cameraStatus; await [Permission.camera] .request (); / .then ((value) {/ / set the result of the application / / cameraStatus=value [Permission.camera]; / /}) / / or directly call: debugPrint ("request permission and obtain permission"); if (await Permission.camera.isDenied) {} / / verify permission if (await Permission.camera.isGranted) {debugPrint ("verify permission: all users agree"); / / users all agree (with & &) / / apply for successful initialization of splash screen _ initSplash () } else if (await Permission.camera.isDenied) {debugPrint ("verify permissions: any set of permissions are denied by the user"); / / the user refused (with |) / / any set of permissions was denied by the user / / spliced prompt permission text StringBuffer sb = new StringBuffer (); sb.write (await Permission.camera.isDenied? "camera,": "); String tip = Utils.removePostfix (sb.toString (),", ") Utils.showCustomDialog (context, ConfirmDialog ("you have rejected the necessary permissions for the application:\ n [$tip], do you want to reapply?", canBackDismiss: false, confirmCallback: () = > _ requestPermission (), cancelCallback: () = > SystemNavigator.pop (),)) } else if (await Permission.camera.isPermanentlyDenied) {debugPrint ("verify permission: permission to deny permanently"); / have permission to permanently deny (with |) / / have any set of permissions to choose not to prompt / / splice prompt permission text StringBuffer sb = new StringBuffer (); sb.write (await Permission.camera.isPermanentlyDenied? "camera,": "); String tip = Utils.removePostfix (sb.toString (),", ") Utils.showCustomDialog (context, ConfirmDialog) ("you have disabled the necessary permissions for the application:\ n [$tip], please allow? in the settings", canBackDismiss: false, confirmText: "go to the settings", confirmCallback: () async {await openAppSettings (); / / Open the settings page SystemNavigator.pop () }, cancelCallback: () = > SystemNavigator.pop (),);}} these are all the contents of the article "how to use dynamic permissions in Flutter development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report