flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

"location.getLocation()" sometimes doesn't work

Open burekas7 opened this issue 1 year ago • 5 comments

Describe the bug Sometimes when running this code: await location.getLocation(); It doesn't continue to the next line and not even caught in a catch block And I don't see anything in the logs.

UPDATE: It seems that sometimes the process just takes him so long for some reason. Why is that?

Versions: 5.0.3 6.0.1

Tested on:

  • Android

burekas7 avatar Apr 24 '24 09:04 burekas7

(Android) when i use location.changeSettings(accuracy: LocationAccuracy.low); then its not working. I try lot but its not working.

Version 6.0.1

Future<(bool, LocationData?)> permissionWithLoction(bool initState) async { Location location = Location();

bool serviceEnabled; PermissionStatus permissionGranted; LocationData? locationData;

try { serviceEnabled = await location.serviceEnabled(); if (!serviceEnabled) { serviceEnabled = await location.requestService(); if (!serviceEnabled) { throw Exception(); } }

permissionGranted = await location.hasPermission();
if (permissionGranted == PermissionStatus.denied) {
  permissionGranted = await location.requestPermission();
  if (permissionGranted.name.toString() != "granted" &&
      permissionGranted.name.toString() != "grantedLimited") {
    permissionGranted = PermissionStatus.denied;
    throw Exception();
  } else {
    await location.changeSettings(accuracy: LocationAccuracy.low);
    locationData = await location.getLocation();
    return (true, locationData);
  }
} else {
  location.changeSettings(accuracy: LocationAccuracy.low);
  locationData = await location.getLocation();

  return (true, locationData);
}

} catch (e) { await openAppSettings(initState); permissionGranted = PermissionStatus.denied; // return (permissionGranted, locationData); } return (false, locationData); }

amanSixDreams avatar May 07 '24 09:05 amanSixDreams

Update: Now it doesn't work on iOS, I don't get any response and the async-await is stuck. Any idea? (iOS 17.5.1)

Version 6.0.2

adiTzoref avatar Aug 21 '24 13:08 adiTzoref

@adiTzoref is 'precise location' turned off? Because that's an issue I noticed as well.

I opened a PR for this.

DennisNoens avatar Aug 23 '24 21:08 DennisNoens

@adiTzoref is 'precise location' turned off? Because that's an issue I noticed as well.

I opened a PR for this.

No. Precise Location in settings is ON. But I think that also when I set it OFF it was the same.

burekas7 avatar Aug 24 '24 08:08 burekas7

Having similar issues, also with precise location ON. Seems it's something with calling the function too early (cubit's constructor), vs app's lifecycle or widget's initState/dispose which seems to work better

Edit: Indeed it seems related to await location.changeSettings. awaiting the changeSettings breaks something.

nadavfima avatar Sep 06 '24 19:09 nadavfima