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.