You are reading the docs for Health 13.x , an older version. Go to 14.x , the latest version.
Specialized types Write menstruation flow, audiograms and insulin delivery using writeMenstruationFlow, writeAudiogram and writeInsulinDelivery.
Menstruation flow, audiograms and insulin delivery each have a dedicated write method. Support for these types varies by platform; see Platform differences .
writeMenstruationFlow writes menstrual cycle data.
Future < bool > writeMenstruationFlow ({
required MenstrualFlow flow ,
required DateTime startTime ,
required DateTime endTime ,
required bool isStartOfCycle ,
RecordingMethod recordingMethod = RecordingMethod . automatic ,
})
Health Connect stores the flow at startTime and HealthKit at endTime, so pass the same time for both. isStartOfCycle marks the first day of a cycle; only iOS stores it.
enum MenstrualFlow {
unspecified ,
none ,
light ,
medium ,
heavy ,
spotting ,
}
Use light, medium or heavy
Only light, medium and heavy are stored as passed on both platforms. On Android, none and spotting throw an ArgumentError. On iOS, unspecified crashes the app, none is stored as unspecified, and spotting is stored as no flow.
// Log menstruation flow
await health . writeMenstruationFlow (
flow : MenstrualFlow . medium ,
startTime : time ,
endTime : time ,
isStartOfCycle : true ,
) ;
// Different flow levels
await health . writeMenstruationFlow (
flow : MenstrualFlow . heavy ,
startTime : DateTime ( 2024 , 1 , 15 , 8 , 0 ) ,
endTime : DateTime ( 2024 , 1 , 15 , 8 , 0 ) ,
isStartOfCycle : false ,
) ;
await health . writeMenstruationFlow (
flow : MenstrualFlow . light ,
startTime : DateTime ( 2024 , 1 , 20 , 10 , 30 ) ,
endTime : DateTime ( 2024 , 1 , 20 , 10 , 30 ) ,
isStartOfCycle : false ,
) ;
This class writes one flow entry per day of a period:
class CycleTracker {
final Health health ;
CycleTracker ( this . health) ;
Future < void > logDailyFlow (
MenstrualFlow flow , {
bool isStartOfCycle = false ,
}) async {
final now = DateTime . now () ;
await health . writeMenstruationFlow (
flow : flow ,
startTime : now ,
endTime : now ,
isStartOfCycle : isStartOfCycle ,
) ;
}
Future < void > logPeriod ({
required DateTime startDate ,
required int durationDays ,
required List < MenstrualFlow > dailyFlow ,
}) async {
for ( int i = 0 ; i < durationDays && i < dailyFlow . length ; i ++ ) {
final date = startDate . add ( Duration (days : i)) ;
await health . writeMenstruationFlow (
flow : dailyFlow[i] ,
startTime : date ,
endTime : date ,
isStartOfCycle : i == 0 ,
) ;
}
}
}
// Usage
final tracker = CycleTracker (health) ;
// Log a 5-day period
await tracker . logPeriod (
startDate : DateTime ( 2024 , 1 , 15 ) ,
durationDays : 5 ,
dailyFlow : [
MenstrualFlow . light ,
MenstrualFlow . medium ,
MenstrualFlow . heavy ,
MenstrualFlow . medium ,
MenstrualFlow . light ,
] ,
) ;
writeAudiogram writes hearing test results. On Android, it throws an UnsupportedError.
Future < bool > writeAudiogram ({
required List < double > frequencies ,
required List < double > leftEarSensitivities ,
required List < double > rightEarSensitivities ,
required DateTime startTime ,
DateTime ? endTime ,
Map < String , dynamic > ? metadata ,
})
// Standard audiogram frequencies
await health . writeAudiogram (
frequencies : [ 125 , 250 , 500 , 1000 , 2000 , 4000 , 8000 ] ,
leftEarSensitivities : [ 20 , 20 , 15 , 15 , 20 , 25 , 30 ] ,
rightEarSensitivities : [ 20 , 15 , 15 , 20 , 20 , 20 , 25 ] ,
startTime : DateTime . now () ,
) ;
// Extended audiogram with more frequencies
await health . writeAudiogram (
frequencies : [
125 , 250 , 500 , 750 , 1000 , 1500 , 2000 , 3000 , 4000 , 6000 , 8000
] ,
leftEarSensitivities : [
25 , 20 , 15 , 15 , 15 , 20 , 20 , 25 , 30 , 35 , 40
] ,
rightEarSensitivities : [
20 , 20 , 15 , 15 , 15 , 15 , 20 , 20 , 25 , 30 , 35
] ,
startTime : DateTime . now () ,
) ;
This example saves an audiogram and classifies each ear by its pure tone average (PTA):
String classifyHearingLoss ( double averageThreshold) {
if (averageThreshold <= 25 ) {
return 'Normal' ;
} else if (averageThreshold <= 40 ) {
return 'Mild hearing loss' ;
} else if (averageThreshold <= 55 ) {
return 'Moderate hearing loss' ;
} else if (averageThreshold <= 70 ) {
return 'Moderately severe hearing loss' ;
} else if (averageThreshold <= 90 ) {
return 'Severe hearing loss' ;
} else {
return 'Profound hearing loss' ;
}
}
class AudiogramAnalyzer {
static double calculatePTA (
List < double > frequencies ,
List < double > sensitivities ,
) {
// Pure Tone Average: average of 500, 1000, 2000 Hz
const ptaFrequencies = [ 500.0 , 1000.0 , 2000.0 ] ;
if ( ! ptaFrequencies . every (frequencies . contains)) return 0 ;
final sum = ptaFrequencies
. map ((f) => sensitivities[frequencies . indexOf (f)])
. reduce ((a , b) => a + b) ;
return sum / ptaFrequencies . length ;
}
static Future < void > saveAndAnalyze (
Health health ,
List < double > frequencies ,
List < double > leftEar ,
List < double > rightEar ,
) async {
// Save audiogram
await health . writeAudiogram (
frequencies : frequencies ,
leftEarSensitivities : leftEar ,
rightEarSensitivities : rightEar ,
startTime : DateTime . now () ,
) ;
// Analyze results
final leftPTA = calculatePTA (frequencies , leftEar) ;
final rightPTA = calculatePTA (frequencies , rightEar) ;
print ( 'Left ear PTA: ${ leftPTA . toStringAsFixed ( 1 )} dB' ) ;
print ( 'Classification: ${ classifyHearingLoss ( leftPTA )} ' ) ;
print ( 'Right ear PTA: ${ rightPTA . toStringAsFixed ( 1 )} dB' ) ;
print ( 'Classification: ${ classifyHearingLoss ( rightPTA )} ' ) ;
}
}
writeInsulinDelivery writes insulin doses. On Android, it throws an UnsupportedError.
Future < bool > writeInsulinDelivery (
double units ,
InsulinDeliveryReason reason ,
DateTime startTime ,
DateTime endTime ,
)
// Bolus insulin dose
await health . writeInsulinDelivery (
5.0 ,
InsulinDeliveryReason . BOLUS ,
time ,
time ,
) ;
// Basal insulin dose
await health . writeInsulinDelivery (
12.0 ,
InsulinDeliveryReason . BASAL ,
time ,
time ,
) ;
enum InsulinType {
rapid ,
short ,
intermediate ,
long ,
}
class InsulinLogger {
final Health health ;
InsulinLogger ( this . health) ;
Future < bool > logDose ({
required double units ,
required InsulinType type ,
required InsulinDeliveryReason reason ,
DateTime ? administeredAt ,
}) async {
final time = administeredAt ?? DateTime . now () ;
final success = await health . writeInsulinDelivery (
units ,
reason ,
time ,
time ,
) ;
if (success) {
print ( 'Logged $ units units of ${ type . name } insulin' ) ;
}
return success ;
}
Future < void > logBasalBolusTherapy ({
required double basalUnits ,
required double bolusUnits ,
}) async {
final now = DateTime . now () ;
// Log long-acting basal
await logDose (
units : basalUnits ,
type : InsulinType . long ,
reason : InsulinDeliveryReason . BASAL ,
administeredAt : now ,
) ;
// Log rapid-acting bolus
await logDose (
units : bolusUnits ,
type : InsulinType . rapid ,
reason : InsulinDeliveryReason . BOLUS ,
administeredAt : now ,
) ;
}
}
// Usage
final logger = InsulinLogger (health) ;
// Meal-time bolus
await logger . logDose (
units : 4.0 ,
type : InsulinType . rapid ,
reason : InsulinDeliveryReason . BOLUS ,
) ;
// Basal-bolus therapy
await logger . logBasalBolusTherapy (
basalUnits : 10.0 ,
bolusUnits : 5.0 ,
) ;
HealthKit stores the units and the delivery reason, not the insulin type: InsulinType only names the dose in the log message.
Request WRITE permission for each specialized type that the platform supports:
// Request permissions for specialized types
final types = [
HealthDataType . MENSTRUATION_FLOW ,
if ( Platform . isIOS) ... [
HealthDataType . AUDIOGRAM ,
HealthDataType . INSULIN_DELIVERY ,
] ,
] ;
final granted = await health . requestAuthorization (
types ,
permissions : List . filled (types . length , HealthDataAccess . WRITE ) ,
) ;
On Android, a request that includes AUDIOGRAM or INSULIN_DELIVERY returns false without showing the permission dialog.
Android iOS
Support for specialized types varies.
Menstruation tracking is supported.
writeAudiogram and writeInsulinDelivery throw an UnsupportedError.
Check Health Connect compatibility.
All specialized types are supported. For the flow values that work, see Flow types .
Menstruation data integrates with Cycle Tracking.
Audiograms appear in the Health app.
Insulin tracking is available.