Piyush Jain
Piyush Jain

Reputation: 1986

Flutter location package is not working in background

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.

Upvotes: 2

Views: 3995

Answers (1)

Joran
Joran

Reputation: 182

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.

Upvotes: 2

Related Questions