Numeric data
Write numeric health data such as weight, vital signs, blood glucose and activity metrics using writeHealthData.
writeHealthData writes a single numeric value of a given type. Request WRITE permission for the type first; see Request write permission.
Signature
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
value | double | Yes | — | Numeric value to write |
unit | HealthDataUnit | No | The type's default unit | Unit of value (iOS only) |
type | HealthDataType | Yes | — | Type of health data |
startTime | DateTime | Yes | — | When the measurement was taken |
clientRecordId | String | No | — | Your own ID for the record (Android only). The plugin ignores it without clientRecordVersion. |
clientRecordVersion | double | No | — | Version of the record with this clientRecordId (Android only), saved as a whole number |
endTime | DateTime | No | startTime | End time (for duration-based metrics) |
recordingMethod | RecordingMethod | No | RecordingMethod.automatic | How the data was recorded. On iOS, it must be manual or automatic; see Set the recording method. |
Pass value in the type's default unit; see Data types. On iOS, you can pass another unit in unit, which Android ignores. The unit must fit the type, for example a mass unit for WEIGHT: for any other unit, HealthKit throws an exception and the app crashes.
Return value
true: the data was written.false: the platform did not save the data, for example because WRITE permission is missing. On Android,writeHealthDataalso returnsfalsefor types it cannot write, such asBODY_MASS_INDEXand the blood pressure types.
Invalid calls and values that Health Connect rejects throw an exception instead; see Error handling.
Supported data types
Body measurements
On Android, writeHealthData returns false for BODY_MASS_INDEX: Health Connect has no BMI record. On iOS, HealthKit measures percentages from 0.0 to 1.0, so 0.185 is 18.5%.
Vital signs
Blood metrics
For blood pressure and blood oxygen, use the dedicated methods in Blood pressure and oxygen.
Activity metrics
Activity metrics cover a duration, so pass both startTime and endTime:
On Android, these types must end after they start: otherwise, the write throws a PlatformException. DISTANCE_DELTA exists only on Android; on iOS, write distance as DISTANCE_WALKING_RUNNING, DISTANCE_CYCLING or DISTANCE_SWIMMING.
Set a client record ID (Android)
writeHealthData has no source parameters: Health Connect and HealthKit record your app as the source. On Android, pass clientRecordId and clientRecordVersion to give the record your own ID:
When you write the same ID again, Health Connect keeps the record with the highest version. iOS ignores both parameters.
Set a time range
For duration-based measurements, pass the start and end of the period:
Set the recording method
recordingMethod says how the data was recorded: automatic (the default), manual, active or unknown. On iOS, it must be manual or automatic, and the plugin sets HKMetadataKeyWasUserEntered to true for manual:
Known issue
On Android, the plugin saves RecordingMethod.manual as actively recorded and RecordingMethod.active as manual entry. A record written with manual reads back as active. To save a manual entry on Android, pass RecordingMethod.active.
Example: weight tracker
This class checks WRITE permission, requests it if needed and then writes the weight:
Validate before writing
Validate values in your app before you write them:
Error handling
writeHealthData throws instead of returning false in these cases:
| Exception | When |
|---|---|
UnsupportedError | Health Connect is not available (Android). |
HealthException | The type does not exist on the platform, such as ELECTRODERMAL_ACTIVITY on Android or DISTANCE_DELTA on iOS. |
ArgumentError | The start time is after the end time, or the type is WORKOUT or ACTIVITY_INTENSITY. On iOS, also when recordingMethod is not manual or automatic, or when the type is ELECTROCARDIOGRAM or a heart rate event type. |
PlatformException | Health Connect rejects the value or the time range (Android), for example a HEIGHT over 3 m or STEPS that do not end after they start. |
Platform differences
- Requires the Health Connect SDK.
- Throws an
UnsupportedErrorif Health Connect is not available. CallisHealthConnectAvailable()before writing; see Health Connect. - Some types may have platform-specific limitations.
- Takes percent values as fractions: 0.185 is 18.5%.
- Data appears immediately in the Health app.
- Without READ permission, reads return only the data your app wrote.
- Converts units automatically (for example, between kg and lbs).
Related
- Workouts: exercise sessions
- Blood pressure and oxygen: systolic and diastolic readings
- Nutrition: meal data
- Data types: all available health data types