简体   繁体   English

Flutter 位置 package 不在后台工作

[英]Flutter location package is not working in background

I am using flutter location package for tracking user location.我正在使用 flutter 位置 package 来跟踪用户位置。 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.我是 flutter 的新手,请帮忙。

On some Android versions, the normal location package keeps working.在某些 Android 版本上,正常位置 package 继续工作。 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.我对background_location package 的体验很糟糕。 The carp_background_location package seemed to work perfectly for me. carp_background_location package 似乎非常适合我。 It is build on top of the more complex background_locator package, which probably also works fine.它建立在更复杂的background_locator package 之上,它可能也可以正常工作。

I would recommend the carp_background_location package.我会推荐carp_background_location package。 It is simple to implement.实现起来很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM