Flutter - 健康包未加载STEP数据

Flutter - Health package not loading STEP data

我在我的 flutter 应用程序中使用了 health-3.0.3 来获取 Google 拟合数据。我能够获取除 STEP 数据之外的所有数据,它始终显示为零。

健康包可以参考这里 Health 3.0.3 Flutter

这是在我的应用程序中访问 STEP 数据类型的代码块

List<HealthDataType> types = [
      HealthDataType.STEPS,
      HealthDataType.WEIGHT,
      //HealthDataType.HEIGHT,
    ];

    setState(() => _state = AppState.FETCHING_DATA);

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

    double steps = 0;

    if (accessWasGranted) {
      try {
        /// Fetch new data
        List<HealthDataPoint> healthData =
            await health.getHealthDataFromTypes(startDate, endDate, 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);

      /// Print the results
      _healthDataList.forEach((x) {
        print("Data point: $x");
        steps += (x.value as double);
      });

      print("Steps: $steps");

您可以参考给定 link 中示例选项卡下的完整代码。有人知道这里出了什么问题吗?

health: 3.0.4 更稳定,当我写这个答案的时候。

来自Android 10.您必须添加ACTIVITY_RECOGNITION才能在AndroidManifest.xml中获得STEP Count权限。

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

然后使用permission_handler请求许可。

if (Platform.isAndroid) {
  final permissionStatus = Permission.activityRecognition.request();
  if (await permissionStatus.isDenied ||
      await permissionStatus.isPermanentlyDenied) {
    showToast(
        'activityRecognition permission required to fetch your steps count');
    return;
  }
}