flutterlocation
flutterlocation copied to clipboard
When listening to location change, calling location.getLocation blocks and does not return any result
When listening to location change, calling location.getLocation blocks and does not return any resultDescription
When listening to location change, calling location.getLocation
blocks and does not return any result.
Expected behavior
It should be ok to call location.getLocation
whether or not we are listening to location change. In that case calling location.getLocation
will return the current location at that point of time and will continue to listen location change as usal.
Steps To Reproduce
Location location = Location();
location.changeSettings(
accuracy: LocationAccuracy.HIGH,
interval: 20000,
distanceFilter: 5
);
Timer.periodic(const Duration(seconds: 2), (timer) async {
currentLocation = await location.getLocation();
print(currentLocation.latitude);
});
location.onLocationChanged.listen((LocationData currentLocation) {
print(currentLocation.longitude);
});
Tested on:
- Android, API Level 31 [e.g. 28], simulator and real device
Im facing this issue right now, have you found any solution or workaround ?
Im facing this issue right now, have you found any solution or workaround ?
No.
This has been an issue for a long time. Dup of #776
Hmm, just a thought but maybe as workaround you could just listen for updates every 500ms or 250ms (or whatever you need), then just store the ‘last known location’ in a variable and later on use that variable
Ultimately I worked around it by using the geolocator package for calls to get location immediately, and location package for background updates. (Geolocator doesn't support background). I don't really like pulling in two packages, but it's not too big of a deal. My location service class wraps them both behind an interface, so I don't really deal with it much after that.
I tried detaching & reattaching the listener whenever I needed an immediate location, but it polluted my code structure too much and I was concerned about reliability.
This location package is great, and one of the few that easily handles location updates in the background. Nice to see it getting some updates recently.
@joe-wd this is something that I'm doing as well. I'm noticing the location
package seems to cause the app to hang a lot, even when simply trying to start/stop background location updates.
Thanks for heads up.