Flutter-geolocator: Error after approving on iOS: PlatformException(PERMISSION_DENIED, Access to location data denied, null)

Created on 30 Apr 2019  ·  12Comments  ·  Source: Baseflow/flutter-geolocator

🐛 Bug Report

  1. Code asks for position, user approves, code throws an error:
    PlatformException(PERMISSION_DENIED, Access to location data denied, null)

  2. But after requesting the position a second time, the correct position is instantly returned without dialog or errors.

Expected behavior

Position is returned and no errors are thrown when asking for position and user approving.

Reproduction steps

import 'package:geolocator/geolocator.dart';
try{
   position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
catch(error)
{
  print("error in geolocation: $error"); //error in geolocation: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
  print("position: $position"); // position: null
  print("retrying");  // retrying
  try{
    position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  }
  catch(error)
  {
    await printGeoStatus(5);
    print("2nd error in geolocation $error");
  }
  print("final position: $position"); //flutter: final position: Lat: 59.xyz, Long: 17.xyz

}

Configuration

Flutter version: "Channel dev, v1.5.8"
Geolocator Version: ^4.0.2

Platform:

  • [x] :iphone: iOS
  • [ ] :robot: Android
ios triage bug

Most helpful comment

I am currently working on a fix for this and a list of other issues (see #430, for more details). It will be a major update though.

Currently iOS side is ready and will work on the Android side now.

All 12 comments

@FlashJonas recently we have completely refactored the geolocator plugin (version 5.0.0) and I was wondering if you still experience this issue in the latest version?

@mvanbeusekom I confirm that the problem still exist in 5.0.0
just discovered your package through AppBrewery courses.

I decided to follow the packages docs and added to my plist file the 3 keys :

<key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs access to location when open.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs access to location when in the background.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>This app needs access to location when open and in the background.</string>

First call to

Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);

prompt the user for permission (with 3 choices) :
image

If I press "only while using the app" the terminal log the following :

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)

0 Geolocator._handleInvalidPermissions (package:geolocator/geolocator.dart:191:7)

1 Geolocator.getCurrentPosition (package:geolocator/geolocator.dart:102:7)

2 _LoadingScreenState.getLocation (package:clima/screens/loading_screen.dart:13:10)

3 _LoadingScreenState.build. (package:clima/screens/loading_screen.dart:23:13)

4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14)

5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:566:30)

6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)

7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9)

8 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177:9)

9<…>

Second press is ok and position is logged without problem :

flutter: Lat: 37.785834, Long: -122.406417

Code :
image

Still have the same issue on my iPhone 5s, 12.2, using geolocator 5.1.2.
*edit (Flutter Channel dev, v1.8.4)

// Called after closing welcome alert
Future getFirstLocation() async {
    Position position;
    try {
      position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    } catch (error) {
      print("getfirstlocation error 1");
      try {
        position = await Geolocator()
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      } catch (error) {
        print("error in geolocation $error");
      }
    }

    print("getfirstlocation position: $position");

    if (position != null) {
     // send to database
    }
  }

Logs from running the above code:
flutter: getfirstlocation error 1
flutter: getfirstlocation position: Lat: 59.xxxx, Long: 17.yyyy

I face the same error:
Android (Oneplus 2) - Android 8.1.0 - Geolocator 5.1.2
(Location permission is granted)

2019-08-15 16:54:22.606 926-1110/system_process E/InputDispatcher: channel '569829a de.paschke.coffeebreak/de.paschke.coffeebreak.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-08-15 16:54:56.808 14735-14766/? E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
    #0      Geolocator._handleInvalidPermissions (package:geolocator/geolocator.dart:207:7)
    #1      Geolocator.getLastKnownPosition (package:geolocator/geolocator.dart:146:7)
    <asynchronous suspension>
    #2      LocationService.getLastKnownLocation (package:coffee_break/services/location_service.dart:24:51)
    <asynchronous suspension>
    #3      UserStoreBase.checkLastLocation (package:coffee_break/stores/user_store.dart:125:36)

I got the same error when used all three permission strings. If I only use NSLocationWhenInUseUsageDescription the app is working again. My best guess is that when using all three permissions, the app is started and asking for location before user is asked to confirm it. The ui behavior seems to confirm it, but it's just a guess. Anyway, if you can live with just NSLocationWhenInUseUsageDescription, then remove the other two.

same issue :( on the last version stable channel of flutter.

this package sucks...

Same error, latetst version of flutter on android 10.

I have the same issue!

I wonder if this issue is going to be addressed, I see it's more than a year old and people still face it. Recently I started learning Flutter and came across with the same issue, with exactly the same steps to reproduce.

When I select "Only while using the app" option from iOS prompt I get an exception in the flutter console even tho the second time I request for the location from the app I get the correct location data.

P.S. using the latest 5.3.2+2 version

I am currently working on a fix for this and a list of other issues (see #430, for more details). It will be a major update though.

Currently iOS side is ready and will work on the Android side now.

This should be fixed in version 6.0.0

It might be late to reply but I was also facing the same problem where the app was not asking for permission in iOS and was working perfectly fine in android.

Because it was not asked for permission that's why the permission code was not working for iOS. I found a package named "location_permissions" which can be used to ask for permission manually.

Steps to do are following

  1. Add "location_permissions: 3.0.0+1" this dependencies in "pubspec.yaml". Please note that I did that for flutter 1.22.0 so for flutter 2.0 this might be an issue.
  2. Import the package in the file

    import 'package:location_permissions/location_permissions.dart';

  3. Add the following code on the page where you want to ask for permission. (Better to add that on the very first page of your app.)

     @override
      void initState() {
       ....
      if (Platform.isIOS) {
        location_permission();
      }
      ....
    

    }

  4. Add the following two methods in the same file

    void location_permission() async {
    final PermissionStatus permission = await _getLocationPermission();
    if (permission == PermissionStatus.granted) {
    final position = await geolocator.getCurrentPosition(
    desiredAccuracy: LocationAccuracy.best);

      // Use the position to do whatever...
    }
    

    }

    Future _getLocationPermission() async {
    final PermissionStatus permission = await LocationPermissions()
    .checkPermissionStatus(level: LocationPermissionLevel.location);

    if (permission != PermissionStatus.granted) {
      final PermissionStatus permissionStatus = await LocationPermissions()
          .requestPermissions(
              permissionLevel: LocationPermissionLevel.location);
    
      return permissionStatus;
    } else {
      return permission;
    }
    

    }

That's it now you should get a popup in the iOS app which will ask for the permission of location.

Was this page helpful?
0 / 5 - 0 ratings