10

I'm having a problem when trying to get the user location using location.getLocation() on a real device, now it works perfectly on the emulator and super fast but on a real device it's slow, I don't know why is that.

the plugin version is ^3.0.2.

anybody has any idea about what's going on here.

Javad Dehban
  • 1,282
  • 3
  • 11
  • 24
John Smith
  • 229
  • 3
  • 11

2 Answers2

8

I had the same issue, but I noticed something.

OBSERVATION 1 Location is off, now I click button to fetch location. It asks me to turn on Location, I turn it on, It takes forever to await location.getLocation(); Execution never reaches next line of code.

OBSERVATION 2 Location is ON, now I click button to fetch location. It doesnot ask to turn on location because it is already on, Guess WHAT, it gets user location in under 3-4 seconds


HOW TO DEAL WITH OBSERVATION 1?

SOLUTION

_locationData = await Future.any([
  location.getLocation(),
  Future.delayed(Duration(seconds: 5), () => null),
]);
if (_locationData == null) {
  _locationData = await location.getLocation();
}

EXPLANATION

I basically kept a timeout of 5 Seconds, if i donot get location in 5 seconds, I discard it returning null & then I hit it again!! & it worked

I have observed this issue in several apps. It is just that the getLocation() function works well if location is already turned on.. this should not happen ideally, there must be some deep down issue with plugin or device hardware.

Hope this helps!

PS : This answer https://stackoverflow.com/a/66108572/7475099 helped me in forming above solution.

krupesh Anadkat
  • 1,932
  • 1
  • 20
  • 31
2

There is a similar issue for iOS devices.

See this comment for more information.

Not found a good solution for this yet.

Seems it might be best to use getLastKnownPosition(), but not sure how well that works if it is the first time you are pulling the location.

James
  • 543
  • 5
  • 14