Skip to content

Overview

The Health plugin provides specialized methods for different data types:

Method Data Types Use Case
writeHealthData Most numeric types General health metrics (weight, height, heart rate, blood glucose, etc.)
writeWorkoutData WORKOUT Exercise sessions with activity types
writeBloodPressure BLOOD_PRESSURE_* Systolic and diastolic readings
writeBloodOxygen BLOOD_OXYGEN Blood oxygen saturation
writeMeal NUTRITION Meals with macros and micronutrients
writeMenstruationFlow MENSTRUATION_FLOW Menstrual cycle tracking
writeAudiogram AUDIOGRAM Hearing test results
writeInsulinDelivery INSULIN_DELIVERY Insulin doses
// 1. Request write permissions
final types = [HealthDataType.WEIGHT];
final permissions = [HealthDataAccess.WRITE];
await health.requestAuthorization(types, permissions: permissions);
// 2. Write the data
final success = await health.writeHealthData(
value: 70.5,
type: HealthDataType.WEIGHT,
startTime: DateTime.now(),
);
if (success) {
print('Weight saved successfully');
} else {
print('Failed to save weight');
}
// Current time (most common)
await health.writeHealthData(
value: 72,
type: HealthDataType.HEART_RATE,
startTime: DateTime.now(),
);
// Specific past time
await health.writeHealthData(
value: 98.6,
type: HealthDataType.BODY_TEMPERATURE,
startTime: DateTime(2024, 1, 15, 8, 30),
);
try {
final success = await health.writeHealthData(
value: 120,
type: HealthDataType.BLOOD_GLUCOSE,
startTime: DateTime.now(),
);
if (success) {
print('Data saved');
} else {
print('Write failed - check permissions');
}
} catch (e) {
print('Error: $e');
}
Future<void> saveMultipleReadings() async {
final readings = [
{'type': HealthDataType.WEIGHT, 'value': 70.5},
{'type': HealthDataType.HEIGHT, 'value': 175.0},
{'type': HealthDataType.BODY_FAT_PERCENTAGE, 'value': 18.5},
];
for (var reading in readings) {
await health.writeHealthData(
value: reading['value'] as double,
type: reading['type'] as HealthDataType,
startTime: DateTime.now(),
);
}
}

Always request WRITE permission before writing:

final writeTypes = [
HealthDataType.WEIGHT,
HealthDataType.HEIGHT,
HealthDataType.STEPS,
];
final granted = await health.requestAuthorization(
writeTypes,
permissions: List.filled(writeTypes.length, HealthDataAccess.WRITE),
);
  • Cannot read previously written data without READ permission
  • Write operations return true on success
  • Data appears immediately in Apple Health app
  • Supports all value types

The plugin performs basic validation:

// ✅ Valid
await health.writeHealthData(
value: 70.5,
type: HealthDataType.WEIGHT,
startTime: DateTime.now(),
);
// ❌ Invalid - negative weight
await health.writeHealthData(
value: -70.5,
type: HealthDataType.WEIGHT,
startTime: DateTime.now(),
); // May fail or be rejected by platform
// ❌ Invalid - future timestamp
await health.writeHealthData(
value: 70.5,
type: HealthDataType.WEIGHT,
startTime: DateTime.now().add(Duration(days: 1)),
); // May fail or be rejected by platform