Flutter-geolocator: Crash on permission denied

Created on 13 Dec 2018  ·  7Comments  ·  Source: Baseflow/flutter-geolocator

🐛 Bug Report

While allowing location permission it's working fine, but while deny the location permission I am getting following error.

Dart Error: Unhandled exception:
PlatformException(PERMISSION_DENIED, Access to location data denied, null)

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

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

2 SignupScreenPageState._submitAction (package:locum_mate/Screens/signup.dart:126:10)

3 SignupScreenPageState.buildWidget.. (package:locum_mate/Screens/signup.dart:261:31)

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

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

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

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

8 TapGestureRecognizer.handlePrimaryPointer (package:flutter<…>

Expected behavior

Expect behaviour should give me null response, so that we can proceed further.

Reproduction steps

Import pub in pubspec.yaml

Write following code to any button tap event
try {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} catch (e) {
print(e);
}

you will have an error.

Configuration

Version: 1.0.1

[✓] Flutter (Channel master, v1.0.1-pre.2, on Mac OS X 10.14 18A384a, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.2)
[✓] VS Code (version 1.29.1)
[✓] Connected device (1 available)

• No issues found!

Platform:

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

Most helpful comment

@YogeshLegendkiller, @kaumudpa, the solution provided by @jaumard is the suggested way of solving this problem.

Another approach would be to use the checkGeolocationPermissionStatus method and make sure the status is set to PermissionStatus.granted before calling the getCurrentPosition, getLastKnownPosition or getPositionStream methods. This would mean however that you need to take care requesting permissions yourself. For example:

Geolocator geolocator = Geolocator();
GeolocationStatus status = await geolocator.checkGeolocationPermissionStatus();
if (status == GeolocationStatus.granted) {
    Position position = await geolocator.getCurrentPosition();
}

All 7 comments

Have you solved this how to handle this

Any workaround to this?

@YogeshLegendkiller @kaumudpa if you want a work around just try catch the call and return null on PlatformException.

@YogeshLegendkiller, @kaumudpa, the solution provided by @jaumard is the suggested way of solving this problem.

Another approach would be to use the checkGeolocationPermissionStatus method and make sure the status is set to PermissionStatus.granted before calling the getCurrentPosition, getLastKnownPosition or getPositionStream methods. This would mean however that you need to take care requesting permissions yourself. For example:

Geolocator geolocator = Geolocator();
GeolocationStatus status = await geolocator.checkGeolocationPermissionStatus();
if (status == GeolocationStatus.granted) {
    Position position = await geolocator.getCurrentPosition();
}

How to ask permission?

@abdullah4one You can use permission_handler and ask for permission like this:

import 'package:permission_handler/permission_handler.dart';

Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);

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

Related issues

dark-chocolate picture dark-chocolate  ·  3Comments

lqmminh picture lqmminh  ·  6Comments

jaumard picture jaumard  ·  3Comments

deisold picture deisold  ·  3Comments

estevez-dev picture estevez-dev  ·  6Comments