Crash on permission denied
🐛 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)
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
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
-
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.
-
Import the package in the file
import 'package:location_permissions/location_permissions.dart'; -
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(); } .... } -
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<PermissionStatus> _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.
Can you please tell how to proceed if I am facing same issue in Android
@harshchan maybe you should create a new issue and explain the problem you are experiencing in detail. This issue is almost 3 years old and a lot has changes since then.
When you create a new issue make sure you mention:
- Full stack trace of the error you receive;
- The contents of your AndroidManifest.xml file (it should contain the permissions);
- A code snippet of the code you are using when the error occurs;
- Clear description of steps you take when your error occurs;
- Output of the
flutter doctor -vcommand.
I had this problem also and the way to solve this by adding this line of code inside the function that tries to get the location: LocationPermission permission = await Geolocator.requestPermission(); So the final function will be:
void getLocation() async { LocationPermission permission = await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.low);
// print(position); }