未为该类定义方法'PermissionHandler'

时间:2020-04-15 07:01:37

标签: flutter dart

PermissionHandler升级到最新版本(5.0.0)之后。找不到PermissionHandler(),而且 The name 'PermissionGroup' isn't a type so it can't be used as a type argument.

这是一个代码段:

  PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
  if (permission != PermissionStatus.granted && permission != PermissionStatus.neverAskAgain) {
    Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
    if (permissions.containsValue(2))
      fileDownload(context, finalUrl);
  } 

出什么问题了?

4 个答案:

答案 0 :(得分:5)

因为permission_handler: ^5.0.0的作者BaseFlow使我们使用起来更加直观 按照this issue ticker (#230)中的要求。

您的代码段需要进行如下更改:

  if (await Permission.storage.request().isGranted) {
      fileDownload(context, finalUrl);
  }

所以现在那些映射如下:

old way                                                 new way
-------                                                 -------

await PermissionHandler()
   .checkPermissionStatus(PermissionGroup.camera)       await PermissionGroup.camera.status

await PermissionHandler().requestPermissions(
   [PermissionGroup.camera]))[PermissionGroup.camera]   await PermissionGroup.camera.request()

await PermissionHandler().requestPermissions(
   [PermissionGroup.camera, PermissionGroup.storage]))  await [PermissionGroup.camera,                                   PermissionGroup.storage].request()

await PermissionHandler().checkServiceStatus
   (PermissionGroup.location)                           await Permission.location.serviceStatus.isEnabled

在此处了解更多信息:this issue ticker (#230)

答案 1 :(得分:3)

仍在努力解决此问题的人,请按照说明进行操作

现在可以处理权限的最新版本是

permission_handler:^ 5.0.0 + hotfix.8

现在我认为他们已经改变了用法。

用例:我必须授予检查联系人权限,才能将联系人保存在电话目录中。所以这对我来说很完美。

  Future<void> saveContactInPhone() async {
    try {

      print("saving Conatct");
      PermissionStatus permission = await Permission.contacts.status;

    if (permission != PermissionStatus.granted) {
      await Permission.contacts.request();
      PermissionStatus permission = await Permission.contacts.status;

      if (permission == PermissionStatus.granted) {
      Contact newContact = new Contact();

        newContact.givenName ="Ajay";
        newContact.familyName ="abc";

          newContact.phones = [Item(label: "mobile",value: "9998887771")];

        await ContactsService.addContact(newContact);

      } else {
        //_handleInvalidPermissions(context);
      }
    } else {
      Contact newContact = new Contact();

        newContact.givenName ="Ajay";
        newContact.familyName ="abc";

          newContact.phones = [Item(label: "mobile",value: "9998887771")];

        await ContactsService.addContact(newContact);
    }
      print("object");      

    } catch (e) {
      print(e);
    }
  }

有关更多信息,请参考Permission_handler_example

答案 2 :(得分:2)

使用Permission_handler:^ 3.0.0,对我有用

答案 3 :(得分:1)

请遵守以下配置

pubspec.yaml

permission_handler: ^6.1.1

导入

   import 'package:permission_handler/permission_handler.dart';

检查权限,如

if (await Permission.storage.request().isGranted) {

  String dir = (await getExternalStorageDirectory()).absolute.path + "/documents";

}