You are reading the docs for Health13.x, an older version. Go to 14.x, the latest version.
Reading data
Choose a read method for raw data points, time buckets or step totals, then filter, sort and process the results.
Copy MarkdownOpen inOpen in GitHubView as MarkdownOpen in Scira AIOpen in ChatGPTOpen in ClaudeOpen in Cursor
The Health class provides four read methods with different levels of granularity and aggregation. One of them, getHealthAggregateDataFromTypes, throws on every call in health 13.x.
Before you read, call configure() and request authorization for the types you read; see Permissions. For the full API, see the API reference on pub.dev.
Data points
getHealthDataFromTypes fetches individual data points for a time range. Use it for detailed raw data, individual measurements, timelines and export or backup.
Interval data
getHealthIntervalDataFromTypes aggregates data into fixed time intervals, such as hourly or daily. Use it for charts, daily, weekly or monthly summaries, trend analysis and statistical overviews.
Aggregate data
getHealthAggregateDataFromTypes never returns data in health 13.x. Use the other read methods for workouts and totals.
Step totals
getTotalStepsInInterval returns the total step count for a period. Use it for step counters, daily step goals, quick step queries and activity badges.
// 1. Configure Healthawait health.configure();// 2. Request permissionsawait health.requestAuthorization([HealthDataType.STEPS]);// 3. Choose reading method based on use casefinal data = await health.getHealthDataFromTypes( types: [HealthDataType.STEPS], startTime: DateTime.now().subtract(Duration(hours: 24)), endTime: DateTime.now(),);// 4. Process resultsfor (var point in data) { print('${point.type}: ${point.value} ${point.unit}');}
getHealthDataFromTypes also accepts a recording-method filter and, per type, a preferred unit. Android converts only a few types and units; see Request preferred units.
final points = await health.getHealthDataFromTypes( types: [ HealthDataType.HEART_RATE, HealthDataType.STEPS, ], startTime: start, endTime: end, // Optional: filter out entries by recording method recordingMethodsToFilter: const [ // RecordingMethod.manual, ], // Optional: request a specific unit per type preferredUnits: { HealthDataType.HEIGHT: HealthDataUnit.METER, },);
getHealthDataByUUID fetches a single HealthDataPoint by its UUID and type. It returns null if no record is found. It also works on Android, where the UUID is the Health Connect record ID.
final dp = await health.getHealthDataByUUID( uuid: '<record-uuid>', type: HealthDataType.WORKOUT,);
Some types normalize their value. For example, sleep and headache types are converted to minutes based on the time interval.
On Android, when you request BODY_MASS_INDEX, the plugin computes one BMI point for each weight in the range, with the latest height in the range. Without a height in the range, it returns no BMI points.
Each HealthDataPoint has a recordingMethod: automatic, manual, active or unknown.
iOS: only automatic and manual are valid for writes. Reads map to those values where available, and recordingMethodsToFilter honors only RecordingMethod.manual.
Android: all four values can be present, based on Health Connect metadata. WORKOUT points and sleep stage points, such as SLEEP_DEEP, always read as unknown, because the plugin does not copy their recording method. The query-time filter still checks the recording method of each Health Connect record.
To exclude recording methods at query time, pass recordingMethodsToFilter:
final data = await health.getHealthDataFromTypes( types: [HealthDataType.STEPS], startTime: start, endTime: end, // Exclude manual entries recordingMethodsToFilter: [RecordingMethod.manual],);
To filter a list you already fetched, filter on the recordingMethod field:
getHealthDataFromTypes and getHealthIntervalDataFromTypes pass their results through removeDuplicates, which keeps the first of each set of equal points in a LinkedHashSet. On iOS, == compares metadata maps by identity, so two reads of the same STEPS or other quantity or category sample never count as duplicates. You can also call removeDuplicates on your own lists:
List<HealthDataPoint> data = await health.getHealthDataFromTypes(...);data = health.removeDuplicates(data);
// Sort by date (newest first)data.sort((a, b) => b.dateTo.compareTo(a.dateTo));// Sort by date (oldest first)data.sort((a, b) => a.dateTo.compareTo(b.dateTo));// Sort by valuedata.sort((a, b) { final aValue = (a.value as NumericHealthValue).numericValue; final bValue = (b.value as NumericHealthValue).numericValue; return bValue.compareTo(aValue);});
temperatureDelta, baseline, measurementLocation, temperature
Check the type before you read its properties:
void processHealthValue(HealthDataPoint point) { final value = point.value; if (value is NumericHealthValue) { print('Numeric: ${value.numericValue}'); } else if (value is WorkoutHealthValue) { print('Workout: ${value.workoutActivityType.name}'); } else if (value is NutritionHealthValue) { print('Meal: ${value.name}'); } else if (value is AudiogramHealthValue) { print('Audiogram: ${value.frequencies.length} frequencies'); } else if (value is ElectrocardiogramHealthValue) { print('ECG: ${value.averageHeartRate} bpm'); } else if (value is InsulinDeliveryHealthValue) { print('Insulin: ${value.units} units'); } else if (value is MenstruationFlowHealthValue) { print('Flow: ${value.flow?.name}'); }}
Read calls throw a HealthException for a type that the plugin does not support on the platform. On Android, they throw an UnsupportedError when Health Connect is not available, but return no points when Health Connect reports an error. getTotalStepsInInterval reports errors differently; see Step totals.
On iOS, HealthKit errors, such as reading while the device is locked, throw a PlatformException:
try { final data = await health.getHealthDataFromTypes( types: [HealthDataType.HEART_RATE], startTime: start, endTime: end, );} on HealthException catch (e) { // Specific health-related errors print('Health error: ${e.dataType} - ${e.cause}');} on PlatformException catch (e) { // Platform-specific errors print('Platform error: ${e.code} - ${e.message}');} catch (e) { // General errors print('Unexpected error: $e');}