health 4.1.1 icon indicating copy to clipboard operation
health: ^4.1.1 copied to clipboard

Wrapper for the iOS HealthKit and Android GoogleFit services.

Health #

Enables reading and writing health data from/to Apple Health and Google Fit.

The plugin supports:

  • handling permissions to access health data using the hasPermissions, requestAuthorization, revokePermissions methods.
  • reading health data using the getHealthDataFromTypes method.
  • writing health data using the writeHealthData method.
  • writing workouts on iOS (Apple Health) & Android (Google Fit) using the writeWorkout method.
  • writing audiograms on iOS using the writeAudiogram method.
  • accessing total step counts using the getTotalStepsInInterval method.
  • cleaning up dublicate data points via the removeDuplicates method.

Note that for Android, the target phone needs to have Google Fit installed and have access to the internet, otherwise this plugin will not work.

Data Types #

Data TypeUnitiOSAndroidComments
ACTIVE_ENERGY_BURNEDCALORIESyesyes
BASAL_ENERGY_BURNEDCALORIESyes
BLOOD_GLUCOSEMILLIGRAM_PER_DECILITERyesyes
BLOOD_OXYGENPERCENTAGEyesyes
BLOOD_PRESSURE_DIASTOLICMILLIMETER_OF_MERCURYyesyes
BLOOD_PRESSURE_SYSTOLICMILLIMETER_OF_MERCURYyesyes
BODY_FAT_PERCENTAGEPERCENTAGEyesyes
BODY_MASS_INDEXNO_UNITyesyes
BODY_TEMPERATUREDEGREE_CELSIUSyesyes
ELECTRODERMAL_ACTIVITYSIEMENSyes
HEART_RATEBEATS_PER_MINUTEyesyes
HEIGHTMETERSyesyes
RESTING_HEART_RATEBEATS_PER_MINUTEyes
STEPSCOUNTyesyes
WAIST_CIRCUMFERENCEMETERSyes
WALKING_HEART_RATEBEATS_PER_MINUTEyes
WEIGHTKILOGRAMSyesyes
DISTANCE_WALKING_RUNNINGMETERSyes
FLIGHTS_CLIMBEDCOUNTyes
MOVE_MINUTESMINUTESyes
DISTANCE_DELTAMETERSyes
MINDFULNESSMINUTESyes
SLEEP_IN_BEDMINUTESyesyes
SLEEP_ASLEEPMINUTESyesyes
SLEEP_AWAKEMINUTESyesyes
WATERLITERyesyesOn Android water requires a 3rd party app to be registered.
EXERCISE_TIMEMINUTESyes
WORKOUTNO_UNITyesyes
HIGH_HEART_RATE_EVENTNO_UNITyesRequires Apple Watch
LOW_HEART_RATE_EVENTNO_UNITyesRequires Apple Watch
IRREGULAR_HEART_RATE_EVENTNO_UNITyesRequires Apple Watch
HEART_RATE_VARIABILITY_SDNNMILLISECONDSyesRequires Apple Watch
HEADACHE_NOT_PRESENTMINUTESyes
HEADACHE_MILDMINUTESyes
HEADACHE_MODERATEMINUTESyes
HEADACHE_SEVEREMINUTESyes
HEADACHE_UNSPECIFIEDMINUTESyes
AUDIOGRAMDECIBEL_HEARING_LEVELyes

Setup #

Apple Health (iOS) #

Step 1: Append the Info.plist with the following 2 entries

<key>NSHealthShareUsageDescription</key>
<string>We will sync your data with the Apple Health app to give you better insights</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We will sync your data with the Apple Health app to give you better insights</string>

Step 2: Enable "HealthKit" inside the "Capabilities" tab.

Google Fit (Android) #

Follow the guide at https://developers.google.com/fit/android/get-api-key

Below is an example of following the guide:

Change directory to your key-store directory (MacOS): cd ~/.android/

Get your keystore SHA1 fingerprint: keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

Example output:

Alias name: androiddebugkey
Creation date: Jan 01, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 4aa9b300
Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033
Certificate fingerprints:
     MD5:  AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9
     SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75
     Signature algorithm name: SHA1withRSA
     Version: 3

Follow the instructions at https://developers.google.com/fit/android/get-api-key for setting up an OAuth2 Client ID for a Google project, and adding the SHA1 fingerprint to that OAuth2 credential.

The client id will look something like YOUR_CLIENT_ID.apps.googleusercontent.com.

Android Permissions #

Starting from API level 28 (Android 9.0) acessing some fitness data (e.g. Steps) requires a special permission.

To set it add the following line to your AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

Additionally, for Workouts: If the distance of a workout is requested then the location permissions below are needed.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

There's a debug, main and profile version which are chosen depending on how you start your app. In general, it's sufficient to add permission only to the main version.

Beacuse this is labled as a dangerous protection level, the permission system will not grant it automaticlly and it requires the user's action. You can prompt the user for it using the permission_handler plugin. Follow the plugin setup instructions and add the following line before requsting the data:

await Permission.activityRecognition.request();

Android X #

Replace the content of the android/gradle.properties file with the following lines:

org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true

Usage #

See the example app for detailed examples of how to use the Health API.

The Health plugin is used via the HealthFactory class using the different methods for handling permissions and getting and adding data to Apple Health / Google Fit. Below is a simplified flow of how to use the plugin.

  // create a HealthFactory for use in the app
  HealthFactory health = HealthFactory();

  // define the types to get
  var types = [
    HealthDataType.STEPS,
    HealthDataType.BLOOD_GLUCOSE,
  ];

  // requesting access to the data types before reading them
  bool requested = await health.requestAuthorization(types);

  var now = DateTime.now();

  // fetch health data from the last 24 hours
  List<HealthDataPoint> healthData = await health.getHealthDataFromTypes(
     now.subtract(Duration(days: 1)), now, types);

  // request permissions to write steps and blood glucose
  types = [HealthDataType.STEPS, HealthDataType.BLOOD_GLUCOSE];
  var permissions = [
      HealthDataAccess.READ_WRITE,
      HealthDataAccess.READ_WRITE
  ];
  await health.requestAuthorization(types, permissions: permissions);

  // write steps and blood glucose
  bool success = await health.writeHealthData(10, HealthDataType.STEPS, now, now);
  success = await health.writeHealthData(3.1, HealthDataType.BLOOD_GLUCOSE, now, now);

  // get the number of steps for today
  var midnight = DateTime(now.year, now.month, now.day);
  int? steps = await health.getTotalStepsInInterval(midnight, now);

Health Data #

A HealthDataPoint object contains the following data fields:

HealthValue value; // NumericHealthValue, AudiogramHealthValue, WorkoutHealthValue
HealthDataType type;
HealthDataUnit unit;
DateTime dateFrom;
DateTime dateTo;
PlatformType platform;
String uuid, deviceId;
String sourceId;
String sourceName;

A HealthData object can be serialized to JSON with the toJson() method.

Fetch health data #

See the example here on pub.dev, for a showcasing of how it's done.

NB for iOS: The device must be unlocked before Health data can be requested, otherwise an error will be thrown:

flutter: Health Plugin Error:
flutter: 	PlatformException(FlutterHealth, Results are null, Optional(Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" UserInfo={NSLocalizedDescription=Protected health data is inaccessible}))

Filtering out duplicates #

If the same data is requested multiple times and saved in the same array duplicates will occur.

A single data point can be compared to each other with the == operator, i.e.

HealthDataPoint p1 = ...;
HealthDataPoint p2 = ...;
bool same = p1 == p2;

If you have a list of data points, duplicates can be removed with:

List<HealthDataPoint> points = ...;
points = Health.removeDuplicates(points);

Workouts #

As of 4.0.0 Health supports adding workouts to both iOS and Android.

Workout Types #

Workout TypeiOSAndroidComments
ARCHERYyesyes
BADMINTONyesyes
BASEBALLyesyes
BASKETBALLyesyes
BIKINGyesyeson iOS this is CYCLING, but name changed here to fit with Android
BOXINGyesyes
CRICKETyesyes
CURLINGyesyes
ELLIPTICALyesyes
FENCINGyesyes
AMERICAN_FOOTBALLyesyes
AUSTRALIAN_FOOTBALLyesyes
SOCCERyesyes
GOLFyesyes
GYMNASTICSyesyes
HANDBALLyesyes
HIGH_INTENSITY_INTERVAL_TRAININGyesyes
HIKINGyesyes
HOCKEYyesyes
SKATINGyesyesOn iOS this is skating_sports
JUMP_ROPEyesyes
KICKBOXINGyesyes
MARTIAL_ARTSyesyes
PILATESyesyes
RACQUETBALLyesyes
RUGBYyesyes
RUNNINGyesyes
ROWINGyesyes
SAILINGyesyes
CROSS_COUNTRY_SKIINGyesyes
DOWNHILL_SKIINGyesyes
SNOWBOARDINGyesyes
SOFTBALLyesyes
SQUASHyesyes
STAIR_CLIMBINGyesyes
SWIMMINGyesyes
TABLE_TENNISyesyes
TENNISyesyes
VOLLEYBALLyesyes
WALKINGyesyes
WATER_POLOyesyes
YOGAyesyes
BOWLINGyes
CROSS_TRAININGyes
TRACK_AND_FIELDyes
DISC_SPORTSyes
LACROSSEyes
PREPARATION_AND_RECOVERYyes
FLEXIBILITYyes
COOLDOWNyes
WHEELCHAIR_WALK_PACEyes
WHEELCHAIR_RUN_PACEyes
HAND_CYCLINGyes
CORE_TRAININGyes
FUNCTIONAL_STRENGTH_TRAININGyes
TRADITIONAL_STRENGTH_TRAININGyes
MIXED_CARDIOyes
STAIRSyes
STEP_TRAININGyes
FITNESS_GAMINGyes
BARREyes
CARDIO_DANCEyes
SOCIAL_DANCEyes
MIND_AND_BODYyes
PICKLEBALLyes
CLIMBINGyes
EQUESTRIAN_SPORTSyes
FISHINGyes
HUNTINGyes
PLAYyes
SNOW_SPORTSyes
PADDLE_SPORTSyes
SURFING_SPORTSyes
WATER_FITNESSyes
WATER_SPORTSyes
TAI_CHIyes
WRESTLINGyes
AEROBICSyes
BIATHLONyes
CALISTHENICSyes
CIRCUIT_TRAININGyes
CROSS_FITyes
DANCINGyes
DIVINGyes
ELEVATORyes
ERGOMETERyes
ESCALATORyes
FRISBEE_DISCyes
GARDENINGyes
GUIDED_BREATHINGyes
HORSEBACK_RIDINGyes
HOUSEWORKyes
INTERVAL_TRAININGyes
IN_VEHICLEyes
KAYAKINGyes
KETTLEBELL_TRAININGyes
KICK_SCOOTERyes
KITE_SURFINGyes
MEDITATIONyes
MIXED_MARTIAL_ARTSyes
P90Xyes
PARAGLIDINGyes
POLOyes
ROCK_CLIMBING(yes)yeson iOS this will be stored as CLIMBING
RUNNING_JOGGING(yes)yeson iOS this will be stored as RUNNING
RUNNING_SAND(yes)yeson iOS this will be stored as RUNNING
RUNNING_TREADMILL(yes)yeson iOS this will be stored as RUNNING
SCUBA_DIVINGyes
SKATING_CROSS(yes)yeson iOS this will be stored as SKATING
SKATING_INDOOR(yes)yeson iOS this will be stored as SKATING
SKATING_INLINE(yes)yeson iOS this will be stored as SKATING
SKIING_BACK_COUNTRYyes
SKIING_KITEyes
SKIING_ROLLERyes
SLEDDINGyes
STAIR_CLIMBING_MACHINEyes
STANDUP_PADDLEBOARDINGyes
STILLyes
STRENGTH_TRAININGyes
SURFINGyes
SWIMMING_OPEN_WATERyes
SWIMMING_POOLyes
TEAM_SPORTSyes
TILTINGyes
TREADMILLyes
VOLLEYBALL_BEACHyes
VOLLEYBALL_INDOORyes
WAKEBOARDINGyes
WALKING_FITNESSyes
WALKING_NORDICyes
WALKING_STROLLERyes
WALKING_TREADMILLyes
WEIGHTLIFTINGyes
WHEELCHAIRyes
WINDSURFINGyes
ZUMBAyes
OTHERyesyes
224
likes
120
pub points
95%
popularity

Publisher

verified publisher iconcachet.dk

Wrapper for the iOS HealthKit and Android GoogleFit services.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

device_info_plus, flutter, intl

More

Packages that depend on health