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

[Question]: Windows 11 PlatformException when calling checkPermission method

Open Dripcoding opened this issue 5 months ago • 0 comments

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

  • [ ] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] macOS
  • [ ] Web
  • [X] Windows

Your question

hello, I'm running a flutter app on Windows 11 and keep encountering the error when calling Geolocation.checkPermission()

PlatformException (PlatformException(UNKNOWN_ERROR, RequestAccess failed. Check the Geolocation Service is running., null, null))

I have enabled Location Services on my local Windows 11 machine and allowed desktop apps to access my location. Is there some configuration I need to add to my flutter app or another Windows 11 setting I need to enable?

Here is my code for reference:

bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('Location services are disabled.');
    }

    LocationPermission permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error('Location permissions are denied');
      }
    }

    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }

    try {
      Position position = await Geolocator.getCurrentPosition(
          locationSettings: const LocationSettings(
              accuracy: LocationAccuracy.bestForNavigation));

      List<Placemark> placemarks =
          await placemarkFromCoordinates(position.latitude, position.longitude);
      // Process placemarks as needed
      return placemarks.first.locality ?? 'Unknown location';
    } catch (e) {
      print('Error: $e');
      throw Exception('Failed to get location');
    }

Version

13.0.1

Dripcoding avatar Sep 28 '24 18:09 Dripcoding