Search code examples
fluttergoogle-fitgoogle-fit-apiflutter-health

Flutter - Health package not loading STEP data


I've used health-3.0.3 in my flutter application to get the Google fit data. I'm able to get every data other than STEP-data which shows zero always.

You can refer to the health package here Health 3.0.3 Flutter

This is the code block to access the STEP datatype in my application

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");

You can refer to the full code under the examples tab in the given link. Does anyone know what's wrong here?


Solution

  • health: 3.0.4 is more stable, when I'm writing this answer.

    From Android 10. you have to add ACTIVITY_RECOGNITION for getting STEP Count permission in AndroidManifest.xml.

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

    And then using permission_handler ask for permission.

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