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

1,470

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, no time to go into that much detail..

Hope this helps brother, Cheers!

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

Share:
1,470
John Smith
Author by

John Smith

Updated on December 24, 2022

Comments

  • John Smith
    John Smith over 1 year

    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.