后台服务问题:使用地理定位器时应用程序崩溃

Background service issue : App crush when using geolocator

我正在开发一个 fitness/health flutter 应用程序项目。

我的应用程序在 android 上运行正常,但在 ios 调用位置服务时应用程序崩溃并立即停止工作。

我在地图视图页面中有一个按钮,用于开始计算步数和步行时间。

我的按钮代码

  RaisedButton(
                          textColor: Colors.white,
                          color: checkRun == false
                              ? Settings.mainColor()
                              : Colors.red,
                          child: Container(
                              padding: EdgeInsets.all(15),
                              child: checkRun == false
                                  ? Text(allTranslations.text("startNow"))
                                  : Text(allTranslations.text("endNow"))),
                          onPressed: () async {
                            rightButtonPressed();
                            if (checkRun == false) {
                              getLocation();
                            } else if (checkRun == true) {
                              setState(() {
                                checkRun = false;
                              });
                              try {
                                FormData formdata = new FormData();
                                // get user token
                                SharedPreferences sharedPreferences =
                                    await SharedPreferences.getInstance();
                                Map<String, dynamic> authUser = jsonDecode(
                                    sharedPreferences
                                        .getString("authUser"));
                                dio.options.headers = {
                                  "Authorization":
                                      "Bearer ${authUser['authToken']}",
                                };
                                formdata.add("startLongitude",
                                    points.first.longitude);
                                formdata.add(
                                    "endLongitude", points.last.longitude);
                                formdata.add(
                                    "startLatitude", points.first.latitude);
                                formdata.add(
                                    "endLatitude", points.last.latitude);
                                formdata.add("date", DateTime.now());

                                meter = distance.as(
                                    lm.LengthUnit.Meter,
                                    lm.LatLng(points.first.latitude,
                                        points.first.longitude),
                                    lm.LatLng(points.last.latitude,
                                        points.last.longitude));
                                setState(() {});

                                print(meter);

                                formdata.add("distance", meter.toInt());
                                formdata.add("steps", _polylineIdCounter);
                                formdata.add("calories", (_polylineIdCounter*0.0512).toInt());

                                response = await dio.post(
                                    "http://104.248.168.117/api/mapInformation",
                                    data: formdata);
                                if (response.statusCode != 200 &&
                                    response.statusCode != 201) {
                                  return false;
                                } else {
                                  print('success -->');
                                  print('Response = ${response.data}');
                                  return true;
                                }
                              } on DioError catch (e) {

                                return false;
                              }
                            }
                           // return true;
                          },
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(20.0)))

getLoaction代码:

 getLocation() {
setState(() {
  checkRun = true;
});
print('CheckRun = > $checkRun');
// Fired whenever a location is recorded
bg.BackgroundGeolocation.onLocation((bg.Location location) {
  print('[location] - $location');
  print('<--------- start onLocation -----------> ');
  print(location.coords.latitude);
  print(location.coords.longitude);
  print('<--------- End onLocation -----------> ');
  if (checkRun == true) {
    setState(() {
      points.add(_createLatLng(
          location.coords.latitude, location.coords.longitude));
      print('Points=> $points');
      _add();
    });
  } else if (checkRun == false) {
    setState(() {
      points.clear();
    });
  }
});

// Fired whenever the plugin changes motion-state (stationary->moving and vice-versa)
bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
  print('[motionchange] - $location');
  print('<--------- Locaiton onMotionChange -----------> ');
  updatelat=location.coords.latitude;
  updatelong=location.coords.longitude;
  setState(() {

  });
  print(location.coords.latitude);
  print(location.coords.longitude);
  print('<--------- / Locaiton onMotionChange -----------> ');
});

// Fired whenever the state of location-services changes.  Always fired at boot
bg.BackgroundGeolocation.onProviderChange((bg.ProviderChangeEvent event) {
});

////
// 2.  Configure the plugin
//
bg.BackgroundGeolocation.ready(bg.Config(
        desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
        distanceFilter: 10.0,
        stopOnTerminate: false,
        startOnBoot: true,
        debug: false,
        logLevel: bg.Config.LOG_LEVEL_INFO,
        reset: true))
    .then((bg.State state) {
  if (!state.enabled) {
    ////
    // 3.  Start the plugin.
    //
    print('[ready] success: $state');
    bg.BackgroundGeolocation.start();
  }
});}

我正在使用这些软件包:

  flutter_background_geolocation: ^1.2.4
    geolocator: ^5.0.1

地图视图在发布版本中非常糟糕,因为 flutter_background_geolocation 需要您购买许可证才能在发布版本中使用,因此当您在调试模式下构建应用程序时,它可以正常工作,但是它会在没有许可证的情况下使用发布模式。