skip to Main Content

I am using the permission_handler which is working fine, but now I need to know if a certain Permission has been requested before.

So I am looking for something like PermissionStatus.notDetermined. Does this package offer something like this? I couldn’t find it, but it is at least for me quite an important thing to know.

I know there is something similar in the firebase_messaging package.

I only want to display a pop up, if the the systemPermissionStatus has NOT been requested before.

This can of course also be done with saving some values like "wasRequested" in SharedPreferences for example, but I thought there must be an existing function for this usecase?

Let me know if you need any more information.

Update

I also noticed that status and the returned status from .request is different.

That doesn’t make any sense for me, it’s super confusing.

final stat = await Permission.notification.status; // -> denied
final req = await Permission.notification.request(); // -> permanentlyDenied

2

Answers


  1. Chosen as BEST ANSWER

    as @gopelkujo already pointed out:

    denied can be used as notDetermined

    The wording "denied" is a bit confusing I think..

    To my issue: It was always giving me back denied instead of permanentlyDenied, because I didn't do the configuration correctly. After adding the setup in the Podfile, everything is working as expected.

        # You can remove unused permissions here
        # for more information: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
        # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
          '$(inherited)',
    
          ## dart: PermissionGroup.camera
          'PERMISSION_CAMERA=1',
    
          ## dart: PermissionGroup.photos
          'PERMISSION_PHOTOS=1',
    
          ## dart: PermissionGroup.notification
          'PERMISSION_NOTIFICATIONS=1',
    
          ## dart: PermissionGroup.appTrackingTransparency
          'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
    

  2. You can see the first example in the How to use section of documentation is using Permission.camera.status.isDenied to determine if the permissions is denied or not asked yet. It’s said in the code comment:

    if (status.isDenied) {
      // We haven't asked for permission yet or the permission has been denied before, but not permanently.
    }
    

    So you can use isDenied as yours notDetermined.

    I provide example code that using isDenied for conditioning the popup

    import 'package:flutter/material.dart';
    import 'package:permission_handler/permission_handler.dart';
    
    class PermissionHandlerPage extends StatefulWidget {
      const PermissionHandlerPage({super.key});
    
      @override
      State<PermissionHandlerPage> createState() => _PermissionHandlerPageState();
    }
    
    class _PermissionHandlerPageState extends State<PermissionHandlerPage> {
      String status = '-';
    
      void _showRequestPermissionDialog() {
        AlertDialog alert(BuildContext context) {
          Widget title = const Text('Permission');
    
          Widget content = const Text(
            'This application need location permission to run.',
          );
    
          Widget cancel = OutlinedButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Cancel'),
          );
    
          Future<void> handleRequest() async {
            // Request [locationWhenInUse] permission.
            final status = await Permission.locationWhenInUse.request();
            this.status = status.name;
            setState(() {});
          }
    
          Widget request = ElevatedButton(
            onPressed: handleRequest,
            child: const Text('Request Permission'),
          );
    
          return AlertDialog(
            title: title,
            content: content,
            actions: [cancel, request],
          );
        }
    
        showDialog(context: context, builder: alert);
      }
    
      Future<void> _checkPermission() async {
        final status = await Permission.location.status;
    
        if (status.isDenied) {
          // [Permission.location.status] As per official documentation:
          // "We haven't asked for permission yet or the permission has been
          // denied before, but not permanently."
          //
          // https://pub.dev/packages/permission_handler
          print('Permission is denied or not asked yet.');
    
          // Show dialog to inform user that permission not granted yet.
          _showRequestPermissionDialog();
        } else if (status.isPermanentlyDenied) {
          print('Permission is permanently denied');
        } else if (status.isGranted) {
          print('Permission granted');
        }
    
        this.status = status.name;
        setState(() {});
      }
    
      @override
      void initState() {
        super.initState();
    
        // Check permission at the first time page opened.
        _checkPermission();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('Permission Handler')),
          body: SafeArea(
            child: Center(
              child: Text('Permission status: $status'),
            ),
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search