flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

When listening to location change, calling location.getLocation blocks and does not return any result

Open nasifmdtanjim opened this issue 1 year ago • 7 comments

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

nasifmdtanjim avatar Nov 16 '23 17:11 nasifmdtanjim

Im facing this issue right now, have you found any solution or workaround ?

YaserH25 avatar Mar 03 '24 21:03 YaserH25

Im facing this issue right now, have you found any solution or workaround ?

No.

nasif-m4 avatar Mar 04 '24 05:03 nasif-m4

This has been an issue for a long time. Dup of #776

joe-wd avatar Mar 31 '24 02:03 joe-wd

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

Im-Fran avatar Apr 18 '24 23:04 Im-Fran

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 avatar Apr 18 '24 23:04 joe-wd

@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.

sunderee avatar Apr 24 '24 07:04 sunderee