flutterlocation
flutterlocation copied to clipboard
Location Listener Fails to Resume After Enabling Location Service
Issue Description: Problem: When the location service is turned off, attempting to resume the location listener by calling the provided code snippet does not work as expected. The listener does not resume after turning on the location service.
Expected Behavior: The location listener should resume successfully after turning on the location service, even if it was initially turned off.
Observed Behavior:
If the location service is off when the listener is set up, attempting to resume it later does not work. The listener works as expected when the location service is on before setting up the listener. Manually turning off and on the location service allows the listener to resume. Code Snippet:
locationListener = Location().onLocationChanged.listen((LocationData currentLocation) {
print('Position' + currentLocation.latitude.toString());
});
Steps to Reproduce:
Start the application with the location service turned off. Set up the location listener using the provided code. Turn on the location service. Observe that the listener does not resume as expected.
Additional Information:
The issue persists even after attempting to call the listener setup code again. It appears that there might be an internal issue preventing the listener from properly resuming after the location service is turned on.
It seems a delay makes it work! am trying to find workaround about that
LocationListener() async {
if (locationListener != null)
return;
Future.delayed(
const Duration(seconds: 5),
() {
print('setting up new listener ${locationListener != null}');
locationListener = location.onLocationChanged
.handleError(handleError)
.listen((LocationData currentLocation) {
print('Position' + currentLocation.latitude.toString());
Backpack.instance.set('Position', currentLocation);
});
},
);
}
ok i got it !
await location.getLocation();
adding this line before onLocationChanged
will make it work
I assume this line have ability to know if the hardware is ready after enabling while onLocationChanged
lack of that
LocationListener() async {
if (locationListener != null) return;
await location.getLocation(); // workaround fix late location serviceissue
locationListener = location.onLocationChanged
.handleError(handleError)
.listen((LocationData currentLocation) {
print('Position' + currentLocation.latitude.toString());
Backpack.instance.set('Position', currentLocation);
});
}