FitKit ()

pub package

Flutter plugin for reading health and fitness data. Wraps HealthKit on iOS and GoogleFit on Android.

Usage

To use this plugin, add fit_kit as a dependency in your pubspec.yaml file.

Getting Started

Android

Enable Fitness API and obtain an OAuth 2.0 client ID.

iOS

Enable HealthKit and add NSHealthShareUsageDescription key to the Info.plist file.

Sample Usage

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(),
      );
    }
  }
}

Supported data types

These are currently available data types and their corresponding GoogleFit/HealthKit types.

Data TypeAndroid (GoogleFit)iOS (HealthKit)Unit
HEART_RATETYPE_HEART_RATE_BPMheartRatecount/min
STEP_COUNTTYPE_STEP_COUNT_DELTAstepCountcount
HEIGHTTYPE_HEIGHTheightmeter
WEIGHTTYPE_WEIGHTbodyMasskilogram
DISTANCETYPE_DISTANCE_DELTAdistanceWalkingRunningmeter
ENERGYTYPE_CALORIES_EXPENDEDactiveEnergyBurnedkilocalorie
WATERTYPE_HYDRATIONdietaryWater >= iOS 9liter
STAND_TIMENot supportedappleStandTime >= iOS 13minute
EXERCISE_TIMENot supportedappleExerciseTime >= iOS 9.3minute
SLEEPFitnessActivities.SLEEPsleepAnalysisiOS:
0 - inBed
1 - asleep
2 - awake
Android:
72 - SLEEP
109 - SLEEP_LIGHT
110 - SLEEP_DEEP
111 - SLEEP_REM
112 - SLEEP_AWAKE

BE AWARE

  • 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.

Libraries

fit_kit