flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

MacOS: First time running app, getCurrentPosition() always get PermissionDeniedException even requestPermission() returns "always"

Open yangyuan opened this issue 2 years ago • 0 comments

🐛 Bug Report

Expected behavior

After requestPermission() returns "always", getCurrentPosition() should not get PermissionDeniedException .

Reproduction steps

I copied below code from https://pub.dev/packages/geolocator and put in main.dart

  1. Remove existing permissions (to mimic running it for the first time). Can be done by "System Preferences" > "Security & Privacy" > "Privacy" > "Location Services". Find your app, right click and "Show in Finder" and delete the app.
  2. Run the app and call _determinePosition(). When pop up permission request, click "OK".
  3. Geolocator.requestPermission() returns always.
  4. Geolocator.getCurrentPosition() will give PermissionDeniedException.

Re-run (or hot restart) the app will not have any issues and can retrieve the location properly.

import 'package:geolocator/geolocator.dart';

/// Determine the current position of the device.
///
/// When the location services are not enabled or permissions
/// are denied the `Future` will return an error.
Future<Position> _determinePosition() async {
  bool serviceEnabled;
  LocationPermission permission;

  // Test if location services are enabled.
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    // Location services are not enabled don't continue
    // accessing the position and request users of the 
    // App to enable the location services.
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      // Permissions are denied, next time you could try
      // requesting permissions again (this is also where
      // Android's shouldShowRequestPermissionRationale 
      // returned true. According to Android guidelines
      // your App should show an explanatory UI now.
      return Future.error('Location permissions are denied');
    }
  }
  
  if (permission == LocationPermission.deniedForever) {
    // Permissions are denied forever, handle appropriately. 
    return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.');
  } 

  // When we reach here, permissions are granted and we can
  // continue accessing the position of the device.
  return await Geolocator.getCurrentPosition();
}

Configuration

Version: geolocator 8.2.1 geolocator_apple 2.1.3 flutter 2.10.5 macOS 12.3.1

Platform:

  • [ ] :iphone: iOS
  • [ ] :robot: Android
  • [x] :iphone: MacOS

yangyuan avatar May 13 '22 20:05 yangyuan