kvs-coder/health_kit_reporter

Is there a way to pull a list of workouts?

amatier2 opened this issue · 2 comments

I'm trying to pull a list of workouts from health kit and reading Apple's docs it appears it needs the identifier of either HKWorkoutType or HKWorkoutTypeIdentifier I may be going about this the wrong way but when I added HKWorkoutTypeIdentifier to my list for requesting read permissions there is a toggle for workouts on the health kit pop up in app, but then when I try to read the data from workouts I get the following error message:

Unhandled Exception: PlatformException(<health_kit_reporter.SwiftHealthKitReporterPlugin: 0x2814d8870>, Error in parsing quantity type, Identifier unknown: HKWorkoutTypeIdentifier, null)

Essentially I use await HealthKitReporter.requestAuthorization(readTypes, []); where readTypes are the identifiers I want to use (heart rate, energy, workouts, ect.) to read from. I pass in an empty list for write types because I'm not interested in writing only reading. Then the Quantity types related to readTypes and use it in:

final preferredUnits = await HealthKitReporter.preferredUnits(types);
        for (var preferredUnit in preferredUnits) {
              logger.e('preferredUnit: ${preferredUnit.identifier}');

              final type = QuantityTypeFactory.from( preferredUnit.identifier );

              final quantities = await HealthKitReporter.quantityQuery(type, preferredUnit.unit,  _predicate );

Does this code seem correct and I'm just using the wrong identifiers?

Is there already a built in way to get workouts saved in health kit? Am I going about it the right way to possibly implement this? Is this not currently possible?

Hi @amatier2

Unhandled Exception: PlatformException(<health_kit_reporter.SwiftHealthKitReporterPlugin: 0x2814d8870>, Error in parsing quantity type, Identifier unknown: HKWorkoutTypeIdentifier, null)

This points out, that the quantity type does not know what is HKWorkoutTypeIdentifier. The reason behind is, that workout type is not a quantity type, but...a workout type. So the type and the function you are looking for is WorkoutType.workoutType and HealthKitReporter.workoutQuery(predicate) respectively.

Please refer to the sample app:
add workoutType to read
and
query workouts

This worked! Thanks for the fast reply. I don't think I have any other issues with workouts so I'm going to close this issue.