Flutter location package is not working in background
On some Android versions, the normal location package keeps working. On others, however, it stops all code execution when the app is in the background (not the front-most app).
I had a bad experience with the background_location package. The carp_background_location package seemed to work perfectly for me. It is build on top of the more complex background_locator package, which probably also works fine.
I would recommend the carp_background_location package. It is simple to implement.
Piyush Jain
Updated on December 23, 2022Comments
-
Piyush Jain over 1 year
I am using flutter location package for tracking user location. It is working as expected. Problem comes when I minimize app, it stops giving location data.
I want to continue getting location data even if app is minimized.
This is how I am getting users location.
Future<void> _listenLocation(store) async { print('listen 1'); location.changeSettings( accuracy: LocationAccuracy.high, distanceFilter: 20, interval: 5000); _locationSubscription = location.onLocationChanged.handleError((dynamic err) { setState(() { _error = err.code; }); _locationSubscription.cancel(); }).listen((LocationData currentLocation) { print('listen 2'); setState(() { print('listen 3'); _error = null; _location = currentLocation; print('_location'); print(_location.speed); print(currentLocation.speed); }); }); }
I am new to flutter, please help.
-
Menachem almost 4 yearsDid you request for the permission to use location services?
-
Piyush Jain almost 4 yearsyes. It is working when app is open. added this in manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET"/>
-
Menachem almost 4 yearsAny messages in the console related to the issue?
-
Piyush Jain almost 4 yearsthere is not issue. It just flutter location package is not working in background. I want to understand how can we get location even in background.
-
Menachem almost 4 yearsTry using the flutter_background_geolocation package. link.
-
Piyush Jain almost 4 yearsThanks for link. I have checked it but only issue is, this package is not free. We will have to buy licence for it.
-
Menachem almost 4 yearsMaybe this one then pub.dev/packages/background_location. You can look for packages on pub.dev by basically searching for what you want. Very easy to use, and very useful.
-