flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

fix: getLocation() freeze

Open gmgm60 opened this issue 2 years ago • 5 comments

Description if the user was disable location service on his phone, and just use requestService(), and he click ok, the app freeze at getLocation()

i use this code and check for PermissionStatus & requestService() before i use getLocation()

i use a fix for now by waiting for 1 sec before use getLocation() , and its work for now, maybe it have something with the time that the service needed to be active.

      final PermissionStatus permissionStatus = await location.hasPermission();
      log('serviceEnabled?');
      final isServiceEnabled =await location.requestService();
      log('serviceEnabled = $isServiceEnabled');
      if (permissionStatus == PermissionStatus.granted && isServiceEnabled) {
        log("Location Permission is granted");
        try {
          await Future.delayed(const Duration(seconds: 1));
          final LocationData userLocationData = await location.getLocation();
          log("userLocationData: $userLocationData");

Expected behavior get current user location

Steps To Reproduce

  1. Go to screen request location
  2. Click on ok to active location service
  3. app freeze with no error

Tested on:

  • Android 11, real device

gmgm60 avatar Sep 03 '22 15:09 gmgm60

I am having the same issue. did you manage to solve it?

enriquejuan2 avatar Sep 07 '22 10:09 enriquejuan2

Can you try what happens when you use this code snippet

`try{ var loc = await location.getLocation().timeout(const Duration(seconds: 1)); }catch (e){

}`

Your app should not crash and freeze.

xunreal75 avatar Sep 07 '22 19:09 xunreal75

I am having the same issue. did you manage to solve it?

it work if i wait 1sec between location services activation and using getLocation()

Its just a workaround I think the reason is the time that the hardware take to start the service, and emit the location

gmgm60 avatar Sep 08 '22 04:09 gmgm60

I changed from using this package and moved to https://pub.dev/packages/permission_handler and my issue is solved

enriquejuan2 avatar Sep 08 '22 07:09 enriquejuan2

This package seems that is not maintained anymore https://pub.dev/packages/geolocator it's a good alternative

arzola avatar Sep 11 '22 12:09 arzola