1

我正在尝试创建一个关于定期从 Google Fit API 获取步骤数据并将该数据发送到自定义 API 的应用程序。到目前为止,一切都很好,并且在应用程序打开时工作正常。然后我想让这些事情发生,即使我杀死了应用程序。

为此,我使用了Flutter Foreground Task插件,它运行良好。但是当我尝试在前台调用获取健康数据功能时,插件说“未授予授权。”。我真的不知道为什么会发生这种情况以及我必须做什么。我是 Flutter 的新手,这是我第一次做这样的项目。

注意:我做了一切设置Health Plugin。我得到了 SHA1 密钥并将其添加到 Google API 控制台等。当我在应用程序中使用它时它工作正常。问题是在前台服务方法上使用它。

Foregorund 服务方法(我称之为获取健康数据方法)

class FirstTaskHandler implements TaskHandler {
  @override
  Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
    // You can use the getData function to get the data you saved.
    final customData =
        await FlutterForegroundTask.getData<String>(key: 'Steps');
    print('Steps: $customData');
  }

  @override
  Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
    // Send data to the main isolate.
    GetHealthData().fetchHealthDataMethod(); //This is where i call health plugin to fetch steps data on foreground.
    sendPort?.send(timestamp);
  }

  @override
  Future<void> onDestroy(DateTime timestamp) async {
    // You can use the clearAllData function to clear all the stored data.
    await FlutterForegroundTask.clearAllData();
  }
}

healthData.dart 类(我实现获取健康数据功能的地方)

class GetHealthData extends StatefulWidget {
  const GetHealthData({Key? key}) : super(key: key);
  fetchHealthDataMethod() => createState().fetchHealthData();

  @override
  _GetHealthDataState createState() => _GetHealthDataState();
}

class _GetHealthDataState extends State<GetHealthData> {
  List<HealthDataPoint> _healthDataList = [];
  var nickname;

  @override
  void initState() {
    super.initState();
  }

  Future fetchHealthData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var datasource = prefs.getInt('datasource');
    if (prefs.getString("token") != null && datasource == 1) {
      // get everything from midnight until now
      DateTime current = DateTime.now();
      DateTime dateFrom;
      DateTime dateTo;

      dateFrom = current.subtract(Duration(
        hours: current.hour,
        minutes: current.minute,
        seconds: current.second,
        milliseconds: current.millisecond,
      ));
      dateTo = DateTime.now();

      HealthFactory health = HealthFactory();

      // define the types to get
      List<HealthDataType> types = [
        HealthDataType.STEPS,
      ];

      // you MUST request access to the data types before reading them
      bool accessWasGranted = await health.requestAuthorization(types);

      int steps = 0;

      if (accessWasGranted) {
        try {
          // fetch new data
          List<HealthDataPoint> healthData =
              await health.getHealthDataFromTypes(dateFrom, dateTo, types);

          // save all the new data points
          _healthDataList.addAll(healthData);
        } catch (e) {
          print("Caught exception in getHealthDataFromTypes: $e");
        }

        // filter out duplicates
        _healthDataList = HealthFactory.removeDuplicates(_healthDataList);
        SharedPreferences prefs = await SharedPreferences.getInstance();
        // print the results
        _healthDataList.forEach((x) {
          print("Data point: $x");
          steps += x.value.round();
          Users.steps = steps;
          TempData.tempSteps = steps;
          prefs.setInt(('sharedSteps'), steps);
        });
        await FlutterForegroundTask.saveData(key: 'Steps', value: steps);

        print("Steps: $steps");


      } else {
        print("Authorization not granted");
        
      }
    }
  }

和错误

错误图像

和调试控制台;

Connecting to VM Service at ws://127.0.0.1:50101/GkZr18KQmIA=/ws
[log] 2021-11-09 12:24:16.768249    FlutterForegroundTask started

I/flutter (18179): Data point: HealthDataPoint - value: 19, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 01:08:09.585, dateTo: 2021-11-09 01:09:09.585, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 10, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 01:09:09.585, dateTo: 2021-11-09 01:09:46.051, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 12, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 01:09:46.051, dateTo: 2021-11-09 01:09:53.487, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 9, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 01:15:43.795, dateTo: 2021-11-09 01:16:43.795, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 17, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 06:31:24.190, dateTo: 2021-11-09 06:32:24.190, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 24, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 06:34:48.088, dateTo: 2021-11-09 06:35:48.088, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 10, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 08:25:12.995, dateTo: 2021-11-09 08:26:12.995, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 17, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 08:53:43.038, dateTo: 2021-11-09 08:54:43.038, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 3, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 08:54:43.038, dateTo: 2021-11-09 08:54:45.471, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 15, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 08:56:23.259, dateTo: 2021-11-09 08:57:23.259, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 8, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 09:01:12.205, dateTo: 2021-11-09 09:02:12.205, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 6, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 10:02:14.110, dateTo: 2021-11-09 10:03:14.110, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 3, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 10:03:14.110, dateTo: 2021-11-09 10:03:16.431, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Data point: HealthDataPoint - value: 36, unit: HealthDataUnit.COUNT, dateFrom: 2021-11-09 11:01:47.001, dateTo: 2021-11-09 11:02:59.001, dataType: HealthDataType.STEPS,platform: PlatformType.ANDROIDsourceId: raw:com.google.step_count.cumulative:samsung:SM-A705FN:c74e7372:step_counter  Non-wakeup,sourceName: SM-A705FN,
I/flutter (18179): Steps: 189
74
I/flutter (18179): Authorization not granted

如您所见,当从 initState 调用 fetching 函数时,它可以正常工作。它可以显示步骤数据并可以打印到屏幕上,但是当我从前台调用相同的函数时,它会显示“未授予授权”。

如果您需要更多信息,请向我询问。感谢您的任何建议和帮助。

4

0 回答 0