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)
Expect behaviour should give me null response, so that we can proceed further.
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.
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:
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
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
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.
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 toPermissionStatus.granted
before calling thegetCurrentPosition
,getLastKnownPosition
orgetPositionStream
methods. This would mean however that you need to take care requesting permissions yourself. For example: