Saya diminta izin dari pengguna saat runtime tetapi saya perlu meminta beberapa izin misalnya telepon, kontak, SMS, dll jadi saya terjebak pada cara memanggil metode ini satu demi satu apakah ada cara untuk bertanya setelah satu izin selesai munculan lain terbuka?

      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;
         }
       }    

Ini kelas izin saya, saya ingin memanggil tiga izin ini dan di initState saya dipanggil seperti ini

    @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');
    });
  }          

Tetapi hanya memanggil satu izin jadi bagaimana cara memanggil ketiga izin ini saat runtime?

0
The-A-Team 11 April 2020, 10:25

1 menjawab

Jawaban Terbaik

Ya, Anda dapat meminta beberapa izin sekaligus, cukup perbarui kode Anda seperti ini file PermissionServices Anda akan will

   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;
     }
   }  

Dan initState Anda akan menjadi

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

  Future permissionAcessPhone() {
     PermissionService().requestPermission(onPermissionDenied: () {
        print('Permission has been denied');
     });
  }     
2
Shubham Narkhede 11 April 2020, 08:01