gpt4 book ai didi

flutter - 如何解决Flutter中的PermissionHandler错误?

转载 作者:行者123 更新时间:2023-12-03 03:22:12 28 4
gpt4 key购买 nike

如何解决flutter中的PermissionHandler错误?
错误是:

the method PermissionHandler isn't defined for the type '_AccessContactState'.


    import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

class AccessContacts extends StatefulWidget {
@override
_AccessContactsState createState() => _AccessContactsState();
}

class _AccessContactsState extends State<AccessContacts> {
Iterable<Contact> _contacts;

@override
void initState() {
super.initState();
}

getContacts() async {
PermissionStatus permissionStatus = await _getPermission();
if (permissionStatus == PermissionStatus.granted) {
var contacts = await ContactsService.getContacts();
setState(() {
_contacts = contacts;
});
} else {
throw PlatformException(
code: 'PERMISSION_DENIED',
message: 'Access to location data denied',
details: null,
);
}
}

Future<PermissionStatus> _getPermission() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.contacts);
if (permission != PermissionStatus.granted &&
permission != PermissionStatus.denied) {
Map<PermissionGroup, PermissionStatus> permisionStatus =
await PermissionHandler()
.requestPermissions([PermissionGroup.contacts]);
return permisionStatus[PermissionGroup.contacts] ??
PermissionStatus.unknown;
} else {
return permission;
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Access Contacts example')),
body: _contacts != null
? ListView.builder(
itemCount: _contacts?.length ?? 0,
itemBuilder: (context, index) {
Contact c = _contacts?.elementAt(index);
return ListTile(
leading: (c.avatar != null && c.avatar.length > 0)
? CircleAvatar(
backgroundImage: MemoryImage(c.avatar),
)
: CircleAvatar(child: Text(c.initials())),
title: Text(c.displayName ?? ''),
);
},
)
: CircularProgressIndicator(),
);
}
}`

如何解决flutter中的 PermissionHandler错误?

最佳答案

您的代码是使用permission_handler: 4.4.0构建的
pubspec.yaml中,请指定permission_handler: 4.4.0

dependencies:
flutter:
sdk: flutter
contacts_service: any
permission_handler: 4.4.0

enter image description here

关于flutter - 如何解决Flutter中的PermissionHandler错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61931097/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com