'How exactly to use Health package in Flutter
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");
}
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barracuda |