SoFunction
Updated on 2025-03-08

Use of dynamic permissions for Flutter development

As we all know, Android has changed permissions to dynamic permissions after version 6.0, while iOS has always used dynamic permissions. Therefore, if some dangerous permissions are involved in Flutter application development, it is necessary to apply for dynamic permissions. You can use Flutter'spermission_handler

Basic use

1. Configure permissions

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

<manifest xmlns:andro
    package=".kill_attendance">
        <!-- ApplyAndroidPermissions-->
    <!--Network access-->
    <uses-permission android:name="" />
    <uses-permission android:name=".ACCESS_NETWORK_STATE" />
    <uses-permission android:name=".ACCESS_WIFI_STATE" />

    <!-- Permissions options for the `contacts` group -->
    <uses-permission android:name=".READ_CONTACTS" />
    <uses-permission android:name=".WRITE_CONTACTS" />
    <uses-permission android:name=".GET_ACCOUNTS" />

    <!-- Permissions options for the `storage` group -->
    <uses-permission android:name=".READ_EXTERNAL_STORAGE" />
    <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />

    <!-- Permissions options for the `camera` group -->
    <uses-permission android:name="" />

    <!-- Permissions options for the `location` group -->
    <uses-permission android:name=".ACCESS_FINE_LOCATION" />
    <uses-permission android:name=".ACCESS_COARSE_LOCATION" />
    <uses-permission android:name=".ACCESS_BACKGROUND_LOCATION" />

    <!-- Permissions options for the `microphone` or `speech` group -->
    <uses-permission android:name=".RECORD_AUDIO" />
         <!-- appname,icon -->
    <application
        android:name=""
        android:label="Application Name"
        android:icon="@mipmap/icon">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name=""
                android:value="true" />
            <intent-filter>
                <action android:name=""/>
                <category android:name=""/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Dynamic permission application

At present, this plug-in has been upgraded several versions, and the processing methods of different versions are different, especially the latest 5.0.0 version and previous versions, which have a large difference in usage.

4.3.0

For example, the following is the usage 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 permission

Examples are as follows:

await Map<PermissionGroup, PermissionStatus> map= PermissionHandler().requestPermissions([ Permission list])

Then, the status of obtaining the application permission is as follows:

PermissionStatus contactsPermStatus = await PermissionHandler().checkPermissionStatus();

Here is a complete example:

  ///Request permission  void _requestPermission() async {
    debugPrint("Enter the splash screen page");
    // Application permission    // PermissionStatus storageStatus ;
    PermissionStatus cameraStatus;

     await PermissionHandler().requestPermissions(
          [ ]).then((value) {
             debugPrint("Return:$value");
            // storageStatus=value[];
            cameraStatus=value[];
          });
    debugPrint("Request permissions and obtain permissions: $cameraStatus");

    //Calculate permissions    if (cameraStatus == ) {
      debugPrint("Check permission: The user agrees");
      //The user has agreed (used &&)      ///All permissions are applied to initialize the flash screen successfully      _initSplash();
    } else if ( cameraStatus == ) {
      debugPrint("Check permissions: Any set of permissions are denied by the user");
      //The user rejected (using ||)      ///There is any set of permissions denied by the user      //Splicing prompt permission text      StringBuffer sb = new StringBuffer();
      (cameraStatus ==  ? "camera," : "");
      String tip = ((), ",");

      (
          context,
          ConfirmDialog(
            "You rejected the necessary permissions for the application:\n[$tip], did you reapply?",
            canBackDismiss: false,
            confirmCallback: () => _requestPermission(),
            cancelCallback: () => (),
          ));
    } else if (  cameraStatus == ) {
      debugPrint("Check permissions: Permanently denied with permissions");
      //Permission to permanently refuse (using ||)      ///If you have any permissions, you will no longer prompt      //Splicing prompt permission text      StringBuffer sb = new StringBuffer();
      (cameraStatus ==  ? "camera," : "");
      String tip = ((), ",");

      (
          context,
          ConfirmDialog(
            "You have disabled the necessary permissions for the application:\n[$tip], please go to the settings to allow it?",
            canBackDismiss: false,
            confirmText: "Go to Settings",
            confirmCallback: () async {
              await PermissionHandler().openAppSettings(); //Open the settings page              ();
            },
            cancelCallback: () => (),
          ));
    }

  }

5.0.0

The 5.0.0 version is generally 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 permission
await [Permission list].request();   //You can use then, use permissions to get the status

Get permission status await

Judgment permission status: await | isGranted, etc.

Here are detailed usage examples:

  ///Request permission  void _requestPermission() async {
    debugPrint("Enter the splash screen page");
    // Application permission    // PermissionStatus cameraStatus;

    await [].request();
    // .then((value){
      //Set the result after application        // cameraStatus=value[];
      // });
    // Or call directly:    debugPrint("Request permissions and obtain permissions");
    if(await ){

    }
    
    //Calculate permissions    if (await ) {
      debugPrint("Check permission: The user agrees");
      //The user has agreed (used &&)      ///All permissions are applied to initialize the flash screen successfully      _initSplash();
    } else if ( await ) {
      debugPrint("Check permissions: Any set of permissions are denied by the user");
      //The user rejected (using ||)      ///There is any set of permissions denied by the user      //Splicing prompt permission text      StringBuffer sb = new StringBuffer();
      (await ? "camera," : "");
      String tip = ((), ",");

      (
          context,
          ConfirmDialog(
            "You rejected the necessary permissions for the application:\n[$tip], did you reapply?",
            canBackDismiss: false,
            confirmCallback: () => _requestPermission(),
            cancelCallback: () => (),
          ));
    } else if ( await ) {
      debugPrint("Check permissions: Permanently denied with permissions");
      //Permission to permanently refuse (using ||)      ///If you have any permissions, you will no longer prompt      //Splicing prompt permission text      StringBuffer sb = new StringBuffer();
      ( await  ? "camera," : "");
      String tip = ((), ",");

      (
          context,
          ConfirmDialog(
            "You have disabled the necessary permissions for the application:\n[$tip], please go to the settings to allow it?",
            canBackDismiss: false,
            confirmText: "Go to Settings",
            confirmCallback: () async {
              await openAppSettings(); //Open the settings page              ();
            },
            cancelCallback: () => (),
          ));
    }

  }

refer to:permission_handler

This is the end of this article about the use of dynamic permissions for Flutter development. For more related Flutter dynamic permissions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!