Flutter 使用permission-handler 权限申请(采坑版,修复运行出错的问题)

  • Post author:
  • Post category:其他


今天使用

permission-handler

集成权限申请的问题

首先集成最新版本的permission-hander

#权限管理
  permission_handler: ^8.3.0

flutter pub get 这个不用多说了

结果直接运行报错了,错误内容如下

报错代码为

D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionManager.java:321: ����: �Ҳ�������
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
                                                           ^
  ����:   ���� S
  �: �� VERSION_CODES
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:72: ����: �Ҳ�������
            case Manifest.permission.BLUETOOTH_SCAN:
                                    ^
  ����:   ���� BLUETOOTH_SCAN
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:74: ����: �Ҳ�������
            case Manifest.permission.BLUETOOTH_ADVERTISE:
                                    ^
  ����:   ���� BLUETOOTH_ADVERTISE
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:76: ����: �Ҳ�������
            case Manifest.permission.BLUETOOTH_CONNECT:
                                    ^
  ����:   ���� BLUETOOTH_CONNECT
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:259: ����: �Ҳ�������
                String result = determineBluetoothPermission(context, Manifest.permission.BLUETOOTH_SCAN);
                                                                                         ^
  ����:   ���� BLUETOOTH_SCAN
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:270: ����: �Ҳ�������
                String result = determineBluetoothPermission(context, Manifest.permission.BLUETOOTH_ADVERTISE);
                                                                                         ^
  ����:   ���� BLUETOOTH_ADVERTISE
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:281: ����: �Ҳ�������
                String result = determineBluetoothPermission(context, Manifest.permission.BLUETOOTH_CONNECT);
                                                                                         ^
  ����:   ���� BLUETOOTH_CONNECT
  �: �� permission
D:\flutter\flutter\.pub-cache\hosted\pub.flutter-io.cn\permission_handler-8.3.0\android\src\main\java\com\baseflow\permissionhandler\PermissionUtils.java:370: ����: �Ҳ�������
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && hasPermissionInManifest(context, null, permission )) {
                                                        ^
  ����:   ���� S
  �: �� VERSION_CODES
8 ������

FAILURE: Build failed with an exception.

请教大神得知,需要针对android把相应的sdk版本号升级到31,

修改android-app-build.gradle的版本为31,如果你androidstudio没安装31的sdk的话,点击工具栏上的这个按钮

弹出sdk页面,勾选相应sdk版本和sdk tools的版本进行下载使用即可


运行就不报错了

android这边处理,在android-app-src-main文件夹下面的AndroidManifest里面添加相关的你需要的权限

iOS这边,在ios-runner文件夹下找到info.plist文件,添加你需要的权限

//权限相关
	<!-- Permission options for the `location` group 地图相关-->
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Need location when in use</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Always and when in use!</string>
    <key>NSLocationUsageDescription</key>
    <string>Older devices need location.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Can I haz location always?</string>

    <!-- Permission options for the `camera` group 相机相关-->
    <key>NSCameraUsageDescription</key>
    <string>camera</string>

    <!-- Permission options for the `photos` group 相册相关-->
    <key>NSPhotoLibraryUsageDescription</key>
    <string>photos</string>

然后写代码,请求权限的代码还是很简单的,简洁明了,后续可以根据相应的状态来进行后续的处理,比如定位和获取图片什么的,例子为获取定位的权限

void requestPermission() async {
    var status = await Permission.location.status;
    if (status.isDenied) {
      //权限未获得
      print("权限未获得,开始请求权限");
      Map<Permission, PermissionStatus> statuses = await [
        Permission.location,
      ].request();
      if (statuses[Permission.location] == PermissionStatus.granted) {
        //定位权限获取
        print("定位权限已获取");
      } else if (statuses[Permission.location] == PermissionStatus.denied) {
        //定位权限被拒绝
        print("定位权限被拒绝");
      }
    } else {
      //权限已获得
      print("权限已获得");
    }
  }



版权声明:本文为u013937668原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。