Flutter location package is not working in background

1,533

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.

Share:
1,533
Piyush Jain
Author by

Piyush Jain

Updated on December 23, 2022

Comments

  • Piyush Jain
    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
      Menachem almost 4 years
      Did you request for the permission to use location services?
    • Piyush Jain
      Piyush Jain almost 4 years
      yes. 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
      Menachem almost 4 years
      Any messages in the console related to the issue?
    • Piyush Jain
      Piyush Jain almost 4 years
      there 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
      Menachem almost 4 years
      Try using the flutter_background_geolocation package. link.
    • Piyush Jain
      Piyush Jain almost 4 years
      Thanks for link. I have checked it but only issue is, this package is not free. We will have to buy licence for it.
    • Menachem
      Menachem almost 4 years
      Maybe 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.