Home > Back-end >  2 permission requests appear one after another?
2 permission requests appear one after another?

Time:02-24

My code is supposed to ask two permission right after another but some how it show 2 permission requests one after another.

What do I have to do to fix this?

Future<void> checkContactPermission() async {
  var status = await Permission.contacts.status;
  if (!status.isGranted) {
    PermissionStatus permissionStatus = await Permission.contacts.request();
    if (status.isDenied) {
      PermissionStatus permissionStatus = await Permission.contacts.request();
    }
  }
  if (status.isGranted) {
    var contacts = await ContactsService.getContacts(withThumbnails: false);
    var list = contacts;
    list.shuffle();
    var FamilyMember = (list.first.phones?.first.value);
    await FlutterPhoneDirectCaller.callNumber('$FamilyMember');
  }
}

CodePudding user response:

You are checking the outdated status here

if (status.isDenied) {
  PermissionStatus permissionStatus = await Permission.contacts.request();
}

Refresh the status before using checking again

Future<void> checkContactPermission() async {
  var status = await Permission.contacts.status;
  if (!status.isGranted) {
    PermissionStatus permissionStatus = await Permission.contacts.request();
    status = await Permission.contacts.status;
    if (status.isDenied) {
      PermissionStatus permissionStatus = await Permission.contacts.request();
    }
  }
  if (status.isGranted) {
    var contacts = await ContactsService.getContacts(withThumbnails: false);
    var list = contacts;
    list.shuffle();
    var FamilyMember = (list.first.phones?.first.value);
    await FlutterPhoneDirectCaller.callNumber('$FamilyMember');
  }
}

Or use the result status from

PermissionStatus permissionStatus = await Permission.contacts.request();
  • Related