The Health plugin provides specialized methods for different data types:
MethodData TypesUse Case
writeHealthDataMost numeric typesGeneral health metrics (weight, height, heart rate, blood glucose, etc.)
writeWorkoutDataWORKOUTExercise sessions with activity types
writeBloodPressureBLOOD_PRESSURE_*Systolic and diastolic readings
writeBloodOxygenBLOOD_OXYGENBlood oxygen saturation
writeMealNUTRITIONMeals with macros and micronutrients
writeMenstruationFlowMENSTRUATION_FLOWMenstrual cycle tracking
writeAudiogramAUDIOGRAMHearing test results
writeInsulinDeliveryINSULIN_DELIVERYInsulin doses

Basic Writing Flow

// 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');
}

Common Patterns

Timestamping

// 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),
);

Error Handling

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');
}

Batch Writing

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

Permission Requirements

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

Platform Differences

  • iOS (HealthKit)
  • Android (Health Connect)
  • 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

Data Validation

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

See Also