s4n
s4n

Reputation: 7

Handling Permission Group (Multiple Permissions) at App runtime - Flutter

I am self learning / developing a flutter App (online music streaming) with below listed features;

  - Start application at startup (run at startup),
  - Streams music list constantly from internet having total views / played count,
  - Constantly monitor network connectivity.
  - Plays music through bluetooth device (if connected) and pause music when bluetooth device gets disconnected, 
  - Keep the screen alive when my app is in use,
  - Send local and push notifications to the app users,
  - Keep it at exception from Battery Optimization

To my best knowledge, i will need below permissions;-

Please correct me with above permission names (if wrong), and I will appreciate if required additional permission names is added to the list. I am not having much clear idea about this lacking my background to programming - Haven't attended any course related to programming / coding yet.

I tried to ask required permissions as a Permission Group, below is my code sample;-

I used Permission-Handler package.

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionHandlerWidget extends StatefulWidget {
  const PermissionHandlerWidget({Key? key}) : super(key: key);

  @override
  _PermissionHandlerWidgetState createState() => _PermissionHandlerWidgetState();
}

class _PermissionHandlerWidgetState extends State<PermissionHandlerWidget> {

  void checkPermissions() async {

    Map<Permission, PermissionStatus> statuses = await [
      Permission.ignoreBatteryOptimizations,
      Permission.notification,
      Permission.location,
      Permission.bluetooth,
    ].request();
    // perform custom action

  }

  @override
  void initState() {
    // TODO: implement initState
    checkPermissions();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Above code asks for location and batteryOptimization permission only. When permission for location denied it asks for batteryOptimzation permission, but when batteryOptimization permission is denied - app crashes all the time.

Moreover,

Map<Permission, PermissionStatus> statuses = await [
      Permission.ignoreBatteryOptimizations,
      Permission.notification,
      Permission.location,
      Permission.bluetooth,
    ].request();
    // perform custom action

In the above codes, I am unable to find all the permissions that I listed in my AndroidManifest.xml file.

I found many samples codes / snippets throughout the internet if I need to ask for individual permissions, but I want to ask all related permissions at initState() and got few examples and most of which went over my understanding :). Further, dealing with isgranted(), isdenied(), isdeniedpermanently() and other cases - not able to get it either.

I know its hassle to someone who tries to help / answer but any piece of correct advise can help me understand and implement this successfully in my project.

Thanking in advance !

Upvotes: 0

Views: 3069

Answers (2)

Madhan
Madhan

Reputation: 51

In permission_handler: ^10.2.0 version we can handle group of permissions like,

i.request permissions

 Map<Permission, PermissionStatus> statuses = await [
  Permission.location,
  Permission.camera,
  Permission.sms,
  Permission.storage,
].request();

ii. check permissions

statuses.values.forEach((element) async {
  if (element.isDenied || element.isPermanentlyDenied) {
    await openAppSettings();
  }
  
});

iii. call in init state

@override
void initState() {
requestPermission();
super.initState();
}

iv. over All code

void requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
  Permission.location,
  Permission.camera,
  Permission.sms,
  Permission.storage,
].request();

statuses.values.forEach((element) async {
  if (element.isDenied || element.isPermanentlyDenied) {
    await openAppSettings();
  }
  
});

}



@override
  void initState() {
    requestPermission();
    super.initState();
  }

permissionHandler

Upvotes: 1

In this case, for Android, the permission Permission.notification, is don't needed to be declared in the manifest; and then for the permission Permission.bluetooth, is needed to be declared in manifest, but is not shown as a dialog, is declared in order to use the device's Bluetooth sensor, and the first thing in this case is ask for to the user to turn on the Bluetooth; these are the reason why in your app in Flutter are not shown the dialog permission request.

Anyway, the correct way to ask for permission is ask for them when you are about to use them, not all at the same time.

Your app won't crash if you do something like this

  void checkPermissions() async {

Map<Permission, PermissionStatus> statuses = await [
  Permission.ignoreBatteryOptimizations,
].request();
// perform custom action

}

But in a more useful and cleaner way will be like this:

  void checkPermissions() async {

    var status = await Permission.location.status;
    if(!status.isGranted){
      var status = await Permission.location.request();
      if(status.isGranted){
        var status = await Permission.ignoreBatteryOptimizations.status;
        if(!status.isGranted){
          var status = await Permission.ignoreBatteryOptimizations.request();
          if(status.isGranted){
            //Good, all your permission are granted, do some stuff
          }else{
            //Do stuff according to this permission was rejected
          }
        }
      }else{
        //Do stuff according to this permission was rejected
      }
    }

  }

Happy coding!

Upvotes: 1

Related Questions