【问题标题】:Flutter - Health package not loading STEP dataFlutter - 健康包未加载 STEP 数据
【发布时间】:2021-02-24 03:19:46
【问题描述】:

我在我的 Flutter 应用程序中使用了 health-3.0.3 来获取 Google fit 数据。除了显示为零的 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");

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

【问题讨论】:

  • 您是否使用调试模式确认 accessWasGranted 为真,并且 GoogleFit 应用程序上显示的步数正确?
  • accessWasGranted 总是假的任何更新
  • 我也有同样的问题。你找到解决办法了吗?
  • 是的,accessWasGranted 设置为 true,并且步数数据在 Google fit 上正确显示
  • @WaiKyaw 还没有

标签: flutter google-fit google-fit-api flutter-health


【解决方案1】:

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;
  }
}

【讨论】:

  • 这个答案帮了很多忙,谢谢。
猜你喜欢
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 2021-04-20
  • 2014-08-28
  • 2022-06-13
  • 2016-01-20
  • 2011-07-29
  • 2014-10-02
相关资源
最近更新 更多