flutter 在清单中未找到以下项的权限:[]22 -扑动

v1uwarro  于 2023-06-24  发布在  Flutter
关注(0)|答案(4)|浏览(257)

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

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_handlerhttps://pub.dev/packages/permission_handler)包。
我试过this solutions,但它不工作。

pxq42qpu

pxq42qpu1#

build.gradle文件中检查targetSdkVersion
如果您使用的是Android 11(targetSdkVersion = 30),则需要以不同的方式将此权限写入manifest。您可以尝试this post中讨论的解决方案

brjng4g3

brjng4g32#

对于那些有经验的人来说,
将此权限添加到清单文件中的<清单标签下

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

rks48beu3#

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

slwdgvem

slwdgvem4#

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
}

相关问题