QuisApp / flutter_contacts

MIT License
88 stars 143 forks source link

getContacts crashes when no permissions were granted. #55

Closed MatrixDev closed 2 years ago

MatrixDev commented 2 years ago

FlutterContacts.getContacts() crashes when permissions are not granted.

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{d926061 7692:com.meest.meest_pay/u0a671} (pid=7692, uid=10671) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

It is not always a good practice to ask for permissions in advance (for ex. if we need to sync during app start) and storing somewhere a flag whether permissions were granted before is a not a good idea either as they can be revoked.

Suggestion (any or both):

  1. check whether permissions are granted before running a query and return empty list or throw Flutter (not Java) exception when not
  2. add ability to check whether permissions are granted - FlutterContacts.isPermissionGranted()
joachim-quis commented 2 years ago

The app developer should check if permissions were granted before using any of the FlutterContacts methods such as FlutterContacts.getContacts(). You can do so directly with:

if (await FlutterContacts.requestPermission()) {
  final contacts = FlutterContacts.getContacts();
  ...
}

which I believe is your suggestion #2.

If permissions were already granted, it won't request them again.

MatrixDev commented 2 years ago

As I stated above - it is not always a good practice to request permission and I couldn't find how to actually check for permissions in this plugin.

joachim-quis commented 2 years ago

it is not always a good practice to request permission

Sorry but what is the alternative? You HAVE TO request permission to be able to access contacts. This is enforced both by Android and iOS.

I couldn't find how to actually check for permissions in this plugin.

As stated above, if permissions were already granted, await FlutterContacts.requestPermission() will silently return true and won't re-request permissions. I believe this is what you want.

MatrixDev commented 2 years ago

check doesn't mean request. I'm asking about checking permissions without actually requesting them. I understand how requestPermission works BTW. What I'm asking is an alternative to https://developer.android.com/reference/android/content/Context#checkSelfPermission(java.lang.String) .

joachim-quis commented 2 years ago

Ah, got it. Currently this is not possible with flutter_contacts, but you can have more fine-grained control over permissions with packages such as permission_handler (https://pub.dev/packages/permission_handler).