Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error 'User denied Geolocation' on web #783

Closed
SeriousMonk opened this issue Jul 10, 2021 · 1 comment · Fixed by #787
Closed

Error 'User denied Geolocation' on web #783

SeriousMonk opened this issue Jul 10, 2021 · 1 comment · Fixed by #787

Comments

@SeriousMonk
Copy link

SeriousMonk commented Jul 10, 2021

It works just fine on mobile. On web, however, if permission is not granted it throws:

Error: User denied Geolocation
    at Object.throw_ [as throw] (http://localhost:58334/dart_sdk.js:5057:11)
    at html_geolocation_manager.HtmlGeolocationManager.new.getCurrentPosition (http://localhost:58334/packages/geolocator_web/src/html_geolocation_manager.dart.lib.js:37:23)
    at getCurrentPosition.throw (<anonymous>)
    at http://localhost:58334/dart_sdk.js:37450:38
    at _RootZone.runBinary (http://localhost:58334/dart_sdk.js:37320:59)
    at _FutureListener.thenAwait.handleError (http://localhost:58334/dart_sdk.js:32561:52)
    at handleError (http://localhost:58334/dart_sdk.js:33094:51)
    at Function._propagateToListeners (http://localhost:58334/dart_sdk.js:33120:17)
    at _Future.new.[_completeError] (http://localhost:58334/dart_sdk.js:32970:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:58334/dart_sdk.js:33006:31)
    at Object._microtaskLoop (http://localhost:58334/dart_sdk.js:37568:13)
    at _startMicrotaskLoop (http://localhost:58334/dart_sdk.js:37574:13)
    at http://localhost:58334/dart_sdk.js:33324:9

This is my code:

permission = await Geolocator.checkPermission();

      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        if (permission == LocationPermission.denied) {
          // Permissions are denied, next time you could try
          // requesting permissions again (this is also where
          // Android's shouldShowRequestPermissionRationale
          // returned true. According to Android guidelines
          // your App should show an explanatory UI now.
          bool? requestAgain = await generateAlertDialog(
            context,
            S.of(context).locationAccessDeniedTitle,
            S.of(context).locationAccessDeniedMsg,
            S.of(context).enableText,
            () => Navigator.pop(context, true),
            leftBtnText: S.of(context).disableText,
            leftBtnOnPressed: () => Navigator.pop(context, false),
          );
          if (requestAgain!) permission = await Geolocator.requestPermission();

          if (permission == LocationPermission.denied) return false;
        }
      }

      if (permission == LocationPermission.deniedForever) {
        // Permissions are denied forever, handle appropriately.
        await generateAlertDialog(
          context,
          S.of(context).locationAccessDeniedForeverTitle,
          S.of(context).locationAccessDeniedForeverMsg,
          S.of(context).okText,
          () => Navigator.pop(context, false)
        );
        return false;
      }

      // When we reach here, permissions are granted and we can
      // continue accessing the position of the device.
      await _getCoordinates();

And this is the method _getCoordinates():

Future<void> _getCoordinates() async{
    context.loaderOverlay.show();

    try {
      getUserInfo.position = await Geolocator.getCurrentPosition();
    } on LocationServiceDisabledException catch (_) {

    }

    _getNearYouRestaurants = _getRestaurantsNearYou();

    context.loaderOverlay.hide();
  }

The problem is that the calls

permission = await Geolocator.checkPermission();
permission = await Geolocator.requestPermission();

are non blocking on web. So the call to

await Geolocator.getCurrentPosition();

is made before permissions are granted/denied and if they are denied it throws error.

Am I doing it wrong or do these calls need to be blocking?

@mvanbeusekom
Copy link
Member

@FRANIAZA thank you for posting this issue. The geolocator_web package indeed contained a bug where it didn't await a future when calling the requestPermission method.

I have just release version 2.0.5 of the geolocator_web package containing the bug fix. If you are depending on the geolocator package you might need to run flutter pub upgrade to make sure you pull in the latest version of the geolocator_web package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants