location.getLocation() is taking too long on a real android device. Flutter
Asked Answered
N

3

12

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.

Nora answered 2/10, 2020 at 12:28 Comment(5)
have you checked permission and turn on location in the device?Erlin
@JavadDehban oh yeah, permissions and everything else is ok, the real device is getting the location eventually, the problem is that it's taking too long to do thatNora
Facing Same issueMelon
Posted a hacky solution below :)Melon
any luck you get this fast ... as i need to stamp location on every photo.. but getting location is taking 2 second every timeForcemeat
M
9

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://mcmap.net/q/414249/-await-future-for-a-specific-time helped me in forming above solution.

Melon answered 22/3, 2021 at 5:38 Comment(1)
3 to 4 second is also a huge time, any finding on thisHypsometer
V
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.

Vu answered 4/12, 2021 at 17:43 Comment(0)
P
0

location.changeSettings( accuracy: LocationAccuracy.low, );check with these make it low or balanced for faster result

Pastime answered 3/9, 2024 at 13:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.