Alternative: https://pub.dev/packages/health
Flutter plugin for reading health and fitness data. Wraps HealthKit on iOS and GoogleFit on Android.
To use this plugin, add fit_kit
as a dependency in your pubspec.yaml file.
Enable Fitness API and obtain an OAuth 2.0 client ID.
Enable HealthKit and add NSHealthShareUsageDescription key to the Info.plist file.
If you're using more than one DataType it's advised to call requestPermissions with all the data types once, otherwise iOS HealthKit will ask to approve every permission one by one in separate screens.
import 'package:fit_kit/fit_kit.dart';
void read() async {
try {
final results = await FitKit.read(
DataType.HEART_RATE,
dateFrom: DateTime.now().subtract(Duration(days: 5)),
dateTo: DateTime.now(),
);
} on UnsupportedException catch (e) {
// thrown in case e.dataType is unsupported
}
}
void readLast() async {
final result = await FitKit.readLast(DataType.HEIGHT);
}
void readAll() async {
if (await FitKit.requestPermissions(DataType.values)) {
for (DataType type in DataType.values) {
final results = await FitKit.read(
type,
dateFrom: DateTime.now().subtract(Duration(days: 5)),
dateTo: DateTime.now(),
);
}
}
}
These are currently available data types and their corresponding GoogleFit/HealthKit types.
Data Type | Android (GoogleFit) | iOS (HealthKit) | Unit |
---|---|---|---|
HEART_RATE | TYPE_HEART_RATE_BPM | heartRate | count/min |
STEP_COUNT | TYPE_STEP_COUNT_DELTA | stepCount | count |
HEIGHT | TYPE_HEIGHT | height | meter |
WEIGHT | TYPE_WEIGHT | bodyMass | kilogram |
DISTANCE | TYPE_DISTANCE_DELTA | distanceWalkingRunning | meter |
ENERGY | TYPE_CALORIES_EXPENDED | activeEnergyBurned | kilocalorie |
WATER | TYPE_HYDRATION | dietaryWater >= iOS 9 | liter |
STAND_TIME | Not supported | appleStandTime >= iOS 13 | minute |
EXERCISE_TIME | Not supported | appleExerciseTime >= iOS 9.3 | minute |
SLEEP | FitnessActivities.SLEEP | sleepAnalysis | iOS: 0 - inBed 1 - asleep 2 - awake Android: 72 - SLEEP 109 - SLEEP_LIGHT 110 - SLEEP_DEEP 111 - SLEEP_REM 112 - SLEEP_AWAKE |
- Calling
await FitKit.read(dataType)
without any extra parameters can lead to FAILED BINDER TRANSACTION on Android devices because of the data batch size being too large.
There's some differences on iOS for these methods:
FitKit.hasPermissions
- false means no, true means user has approved or declined permissions.To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store. https://developer.apple.com/documentation/healthkit/hkhealthstore/1614154-authorizationstatus
FitKit.revokePermissions
- isn't supported by HealthKit, method does nothing.