flutter-permission-handler icon indicating copy to clipboard operation
flutter-permission-handler copied to clipboard

[Bug]: Permission.locationAlways.request doesn't wait for selection in system dialog

Open KrsticM opened this issue 9 months ago • 16 comments

Please check the following before submitting a new issue.

Please select affected platform(s)

  • [ ] Android
  • [x] iOS
  • [ ] Windows

Steps to reproduce

When calling await Permission.locationAlways.request() system dialog is shown but lib is not waiting for a selection, instead it just returns .denied.

Expected results

To wait for a selection in location permission system dialog.

Actual results

PermissionStatus.denied is returned right away.

Code sample

Code sample
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'package:vegetation_manager/common/helper/location_permission_manager.dart';

final class LocationPermissionManagerImpl implements LocationPermissionManager {
  @override
  Future<bool> isLocationAlwaysAllowed() async {
    var locationAlwaysStatus = await Permission.locationAlways.status;

    if (!locationAlwaysStatus.isGranted) {
      final whenInUseStatus = await Permission.locationWhenInUse.request();

      if (whenInUseStatus.isGranted) {
        // Not waiting for user to choose option in dialog, instead returning .denied right away
        locationAlwaysStatus = await Permission.locationAlways.request();
        return locationAlwaysStatus.isGranted;
      }
      return false;
    }
    return true;
  }
}

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

11.4.0

Flutter Doctor output

Doctor output
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.0, on macOS 15.3.1 24D70 darwin-arm64, locale en-RS)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.98.0)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

KrsticM avatar Mar 10 '25 09:03 KrsticM

I run into the same error -- same parameters.

vilorel avatar Mar 10 '25 11:03 vilorel

I got the same error

AndRud avatar Mar 12 '25 09:03 AndRud

Did someone find any solutions?

v1n4ester avatar Mar 12 '25 11:03 v1n4ester

I have made a timer to check for Permission.locationAlways.status every second until it is granted. It's not pretty but it works with some edge cases.

KrsticM avatar Mar 12 '25 11:03 KrsticM

I have made a timer to check for Permission.locationAlways.status every second until it is granted. It's not pretty but it works with some edge cases.

What if user deny the permission?

v1n4ester avatar Mar 12 '25 12:03 v1n4ester

I have made a timer to check for Permission.locationAlways.status every second until it is granted. It's not pretty but it works with some edge cases.

What if user deny the permission?

I'm waiting for 30 seconds, if it's not granted in that period I assume it's denied.

KrsticM avatar Mar 12 '25 12:03 KrsticM

@v1n4ester This is not optimal, as you also said. The location and locationWhenInSure both work, so the locationAlways should work by extension. It's therefore a bug that should be addressed via either a PR or by the community maintaining the plugin.

vilorel avatar Mar 14 '25 00:03 vilorel

any updates? I would imagine this is high priority for many devs

vilorel avatar Mar 26 '25 14:03 vilorel

any updates? I would imagine this is high priority for many devs

Nope, problem still exist

v1n4ester avatar Mar 26 '25 14:03 v1n4ester

I got the same error

spoorknight avatar Apr 03 '25 10:04 spoorknight

Same.

loregr avatar Apr 14 '25 07:04 loregr

similar with my issue on latest version. stuck at calling Permission.locationAlways.request(), not opening permission dialog instead. I got this warning and exception:

W/Activity(17584): Can request only one set of permissions at a time PlatformException(PermissionHandler.PermissionManager, A request for permissions is already running, please wait for it to finish before doing another request (note that you can request multiple permissions at the same time)., null, null)

but no permission dialog is opening before.

agilst avatar May 11 '25 02:05 agilst

Got similar issue while requesting both location and Phone

W/Activity( 7365): Can request only one set of permissions at a time

W/permissions_handler( 7365): onRequestPermissionsResult is called without results. This is probably caused by interfering request codes. If you see this error, please file an issue in flutter-permission-handler, including a list of plugins used by this application: https://github.com/Baseflow/flutter-permission-handler/issues

final status = await [ Permission.location, Permission.phone].request();

permission_handler: 11.3.1 , Flutter: 3.24.3

Bhagavan-0413 avatar May 23 '25 09:05 Bhagavan-0413

I emulated expected behavior for my case by this way. Hope it will help somebody

  bool _whileInUseGranted = false;
  bool _alwaysGranted = false;
  bool _notificationGranted = false;
  Timer? _alwaysPermissionCheckTimer;
  bool checkingAlways = false;

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _checkPermissions();
  }

  @override
  void dispose() {
    _alwaysPermissionCheckTimer?.cancel();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print("state: " + state.toString());
    if (state == AppLifecycleState.resumed) {
      _checkPermissions();
    }
  }

  Future<void> _checkPermissions() async {
    setState(() => _loading = true);

    final locStatus = await Permission.location.status;
    final alwaysStatus = await Permission.locationAlways.status;
    final notifStatus = await Permission.notification.status;

    setState(() {
      _whileInUseGranted = locStatus.isGranted;
      _alwaysGranted = alwaysStatus.isGranted;
      _notificationGranted = notifStatus.isGranted;
      _loading = false;
    });

    if (checkingAlways && alwaysStatus.isPermanentlyDenied) {
      checkingAlways = false;
      _alwaysPermissionCheckTimer?.cancel();
      _showLocationSettingsDialog();
    }

    if (_alwaysGranted && _notificationGranted) {
      _continueToApp();
    }
  }

Future<void> _requestAlwaysPermission() async {
    final res = await Permission.locationAlways.request();

    if (res.isGranted) {
      setState(() => _alwaysGranted = true);
      if (_notificationGranted) _continueToApp();
      return;
    }

    if (res.isDenied) {
      checkingAlways = true;
      _alwaysPermissionCheckTimer = Timer.periodic(Duration(seconds: 1), (
        timer,
      ) async {
        final updatedStatus = await Permission.locationAlways.status;

        if (updatedStatus.isGranted) {
          timer.cancel();
          checkingAlways = false;
          setState(() => _alwaysGranted = true);
          if (_notificationGranted) _continueToApp();
        }
      });

      return;
    }

    if (res.isPermanentlyDenied) {
      _showLocationSettingsDialog();
    }
  }

GoStaRoff avatar Jul 13 '25 21:07 GoStaRoff

In IOS permission pop up is not work in real device i try many times but not working.

SuryaMicrolent01 avatar Aug 12 '25 12:08 SuryaMicrolent01

I got the same error

cvilia avatar Aug 25 '25 04:08 cvilia