Search code examples
flutterdartflutter-dependenciesflutter-packages

Flutter Error: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord


I needed to import contacts from mobile, and I used contacts_service from pub.dev. Then I made the changes required in AndroidManifest.xml for android and in info.plist for iOS, i.e, added the required permission, still I am getting this error when I am trying to read contacts from phone.

 Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord

Solution

  • I resolved this issue, by requesting permission from the user, like this,

      @override
      void initState() {
        super.initState();
        getContacts();
      }
    
     // Function to get permission from the user
      _contactsPermissions() async {
        PermissionStatus permission = await Permission.contacts.status;
        if (permission != PermissionStatus.granted && permission != PermissionStatus.denied) {
          Map<Permission, PermissionStatus> permissionStatus = await [Permission.contacts].request();
          return permissionStatus[Permission.contacts] ?? PermissionStatus.undetermined;
        } else {
          return permission;
        }
      }
    
      //Function to import contacts
      getContacts() async {
        PermissionStatus contactsPermissionsStatus = await _contactsPermissions();
        if (contactsPermissionsStatus == PermissionStatus.granted) {
          List<Contact> _contacts = (await ContactsService.getContacts(withThumbnails: false)).toList();
          setState(() {
            contacts = _contacts;
          });
        }
      }