2

I am using health 3.2.0 https://pub.dev/packages/health package and after all the effort I am able to successfully access the authorisation. The problem I am facing is when I get health data from types using health factory, it is not returning anything and the length of health data points list is 0 every time.

P.S. What is the use of oAuth client id and client_secret.json file that I downloaded when I got the client id. Should I have to place these anywhere in the code?

My code:

  DateTime startDate = DateTime(2021, 10, 10, 0, 0, 0);
  DateTime endDate = DateTime(2025, 11, 07, 23, 59, 59);

  Future fetchHealthData() async {
    HealthFactory healthFactory = new HealthFactory();

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

    bool accessAuthorization =
        await healthFactory.requestAuthorization(types).catchError((e) {
      print("authorization error:");
      print(e.toString());
    });
    print("authorization granted: $accessAuthorization");
    if (accessAuthorization) {
      try {
        // fetch new data
        List<HealthDataPoint> healthData = await healthFactory
            .getHealthDataFromTypes(startDate, endDate, types);

        // printing length of healtData point list, this is 0
        print("health data points length: ${healthData.length}");

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

      // filter out duplicates
      _healthDataPointList =
          HealthFactory.removeDuplicates(_healthDataPointList);
      print("health data entry points: ${_healthDataPointList.length}");

      // print the results
      _healthDataPointList.forEach((x) {
        print("Data point: $x");
        // steps += x.value.round();
      });

      // print("Steps: $steps");

    } else {
      print("Authorization not granted");
    }
  }
1
  • Have you able to find the use of client-id and json file? Commented Oct 27, 2022 at 6:51

1 Answer 1

2

Both android and apple simulators can't simulate health data to be shown, reason why the length of health data points will be always 0.

Try connecting a real device.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.