I was asked permission from the user in runtime but I need to ask MULTIPLE permissions for example phone, contact, SMS, etc so I'm stuck on that how to call this method one after another is there any way to ask after one permission completed another popup open?
import 'package:permission_handler/permission_handler.dart';
class PermissionService{
final PermissionHandler _permissionHandler = PermissionHandler();
Future<bool> _requestPermissionContact(PermissionGroup permission) async {
var result = await _permissionHandler.requestPermissions([permission]);
if (result[PermissionGroup.contacts] == PermissionStatus.granted) {
print("sfhalsjfhas fasdhufaiusfh aloifahdf oalsifu");
}
}
Future<bool> requestContactPermission({Function onPermissionDenied}) async {
var granted = await _requestPermissionContact(PermissionGroup.contacts);
if (!granted) {
onPermissionDenied();
}
return granted;
}
Future<bool> hasContactPermission() async {
return hasPermission(PermissionGroup.contacts);
}
Future<bool> _requestPermissionSms(PermissionGroup permission) async {
var result = await _permissionHandler.requestPermissions([permission]);
if (result[permission] == PermissionStatus.granted) {
print("sfhalsjfhas fasdhufaiusfh aloifahdf oalsifu");
return true;
}
return false;
}
Future<bool> requestSmsPermission({Function onPermissionDenied}) async {
var granted = await _requestPermissionSms(PermissionGroup.sms);
if (!granted) {
onPermissionDenied();
}
return granted;
}
Future<bool> hasSmsPermission() async {
_requestPermissionPhone(PermissionGroup.phone);
return hasPermission(PermissionGroup.sms);
}
Future<bool> _requestPermissionPhone(PermissionGroup permission) async {
var result = await _permissionHandler.requestPermissions([permission]);
if (result[permission] == PermissionStatus.granted) {
return true;
}
return false;
}
Future<bool> requestPhonePermission({Function onPermissionDenied}) async {
var granted = await _requestPermissionPhone(PermissionGroup.phone);
if (!granted) {
onPermissionDenied();
}
return granted;
}
Future<bool> hasPhonePermission() async {
return hasPermission(PermissionGroup.phone);
}
Future<bool> hasPermission(PermissionGroup permission) async {
var permissionStatus =
await _permissionHandler.checkPermissionStatus(permission);
return permissionStatus == PermissionStatus.granted;
}
}
this is my permission class i want to call this three permission and in initState i was called this like
@override
initState() {
permissionAcess();
permissionAcessSms();
permissionAcessPhone();
super.initState();
}
Future permissionAcess() {
PermissionService().requestContactPermission(onPermissionDenied: () {
print('Permission has been denied');
});
}
Future permissionAcessSms() {
PermissionService().requestSmsPermission(onPermissionDenied: () {
print('Permission has been denied');
});
}
Future permissionAcessPhone() {
PermissionService().requestPhonePermission(onPermissionDenied: () {
print('Permission has been denied');
});
}
but it called only one permission so how to call all these three permission in runtime?
Yes, you can ask multiple permissions at the same time. Just update your PermissionServices
class:
import 'package:permission_handler/permission_handler.dart';
class PermissionService{
final PermissionHandler _permissionHandler = PermissionHandler();
Future<bool> _requestPermission() async {
var result = await _permissionHandler.requestPermissions([PermissionGroup.phone,PermissionGroup.contacts,PermissionGroup.sms]);
if (result == PermissionStatus.granted) {
return true;
}
return false;
}
Future<bool> requestPermission({Function onPermissionDenied}) async {
var granted = await _requestPermission();
if (!granted) {
onPermissionDenied();
}
return granted;
}
Future<bool> hasPhonePermission() async {
return hasPermission(PermissionGroup.phone);
}
Future<bool> hasPermission(PermissionGroup permission) async {
var permissionStatus =
await _permissionHandler.checkPermissionStatus(permission);
return permissionStatus == PermissionStatus.granted;
}
}
And your initState
:
@override
initState(){
permissionAcessPhone();
super.initState();
}
Future permissionAcessPhone() {
PermissionService().requestPermission(onPermissionDenied: () {
print('Permission has been denied');
});
}