在清单中找不到以下权限:[]22 - Flutter

问题描述 投票:0回答:6

我正在尝试从用户那里获取存储权限。以下是示例(复制粘贴)代码。但是当我尝试请求权限时出现错误。

D/permissions_handler(12775): No permissions found in manifest for: []22

代码

import 'package:duplicate_file_remover/globals.dart' as globals;
import 'package:duplicate_file_remover/ui/views/homeViews/homeView.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart';

class MainDataProvider extends StatefulWidget {
  const MainDataProvider({Key? key}) : super(key: key);

  @override
  _MainDataProviderState createState() => _MainDataProviderState();
}

class _MainDataProviderState extends State<MainDataProvider> {

  PermissionStatus _permissionStatus = PermissionStatus.denied;

  Future<void> _askStoragePermission() async {
    debugPrint(" ---------------- Asking for permission...");
    await Permission.manageExternalStorage.request();
    if (await Permission.manageExternalStorage.request().isGranted) {
      PermissionStatus permissionStatus =
          await Permission.manageExternalStorage.status;
      setState(() {
        _permissionStatus = permissionStatus;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () async {
            await _askStoragePermission();

            if (_permissionStatus.isGranted) {
              debugPrint(" ---------------- Permission allowed");
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => HomeView(),
                ),
              );
            } else {
              // openAppSettings();
              debugPrint(" --------------------- Permission denied");
            }
          },
          child: const Text("Get permission"),
        ),
      ),
    );
  }
}

我正在使用

permission_handler
(https://pub.dev/packages/permission_handler) 包。

我尝试了这个解决方案,但它不起作用。

flutter permissions android-permissions
6个回答
3
投票

检查

targetSdkVersion
文件中的
build.gradle

如果您使用的是 Android 11 (targetSdkVersion = 30),则需要以不同方式在

manifest
中写入此权限。您可以尝试这篇文章

中讨论的解决方案

2
投票

对于那些也经历过这个问题的人来说,

将此权限添加到清单文件中的

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

1
投票
Map<Permission, PermissionStatus> statuses = await [
  Permission.storage,
  Permission.manageExternalStorage,
].request();

var storage = statuses[Permission.storage];
var manageExternalStorage = statuses[Permission.manageExternalStorage];
if (storage.isGranted || manageExternalStorage.isGranted) {
  // do something
}

0
投票

我也遇到过同样的问题。看起来 file_picker 本身处理存储权限。因此您无需单独请求此许可。只需直接使用

pickFile()
函数,插件就会处理剩下的事情。 希望有帮助


0
投票

这里的根本问题可能是 Android 在 v13 之后所做的存储权限更改。 因此应用程序代码需要支持这两种权限类型。

第一个清单文件

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后像下面这样的代码处理两个流程,就像 Even Cai 在他们的答案中分享的那样。

void someFunction() async {
  Map<Permission, PermissionStatus> statuses = await [
    Permission.storage,
    Permission.manageExternalStorage,
  ].request();

  PermissionStatus? storage = statuses[Permission.storage];
  PermissionStatus? manageExternalStorage = statuses[Permission.manageExternalStorage];

  bool permitted = await tryPermission(storage) || await tryPermission(manageExternalStorage); // for android <13 and 13+

  if (permitted) {
    return await writeFile(file, response.bodyBytes);
  } else {
    openAppSettings(); // non-blocking intended
    throw CustomException("Failed to get storage permission");
  }
}

Future<bool> tryPermission(PermissionStatus? status) async {
  if (status != null) {
    if (status.isGranted) {
      print('granted');
      return true;
    } else if (status.isDenied) {
      print('requesting permission');
      status = await Permission.manageExternalStorage.request();
      if (status.isGranted) {
        print('newly granted');
        return true;
      } else {
        print('User denied storage permission request');
        return false;
      }
    } else if (status.isPermanentlyDenied) {
      print('app settings open');      
      return false;
    }
  }
  return false;
}

-1
投票

我有同样的错误,我通过创建一个新的 flutter 项目并将我的代码复制到新的来解决它

© www.soinside.com 2019 - 2024. All rights reserved.