Skip to content

Quickstart

First, import the package and create a Health instance:

import 'package:health/health.dart';
final health = Health();
await health.configure();

Request basic read permissions for common health data:

final types = [
HealthDataType.STEPS,
HealthDataType.HEART_RATE,
];
final granted = await health.requestAuthorization(types);
if (granted) {
print('✓ Permissions granted!');
} else {
print('✗ Permissions denied');
}
Health Connect Setup (Android Only)

On Android, users need Google Health Connect installed:

// Check if Health Connect is available
final available = await health.isHealthConnectAvailable();
if (!available) {
// Open Play Store to install Health Connect
await health.installHealthConnect();
}
// Check detailed status
final status = await health.getHealthConnectSdkStatus();
print('Health Connect status: $status');

Read your step count from the last 24 hours:

final now = DateTime.now();
final yesterday = now.subtract(const Duration(hours: 24));
final healthData = await health.getHealthDataFromTypes(
types: [HealthDataType.STEPS],
startDate: yesterday,
endDate: now,
);
for (var point in healthData) {
print('Steps: ${point.value}');
}

Get Today's Steps

Use the optimized method for step counting:

final now = DateTime.now();
final midnight = DateTime(now.year, now.month, now.day);
final steps = await health.getTotalStepsInInterval(midnight, now);
print('Steps today: ${steps ?? 0}');

Save a simple health measurement like weight:

final now = DateTime.now();
final success = await health.writeHealthData(
value: 70.5, // 70.5 kg
type: HealthDataType.WEIGHT,
startTime: now,
);
if (success) {
print('✓ Weight saved!');
} else {
print('✗ Failed to save weight');
}
Remove Health Data

Delete data you’ve previously written:

final now = DateTime.now();
final yesterday = now.subtract(Duration(hours: 24));
final success = await health.delete(
type: HealthDataType.STEPS,
startTime: yesterday,
endTime: now,
);
if (success) {
print('✓ Data deleted');
}
import 'package:flutter/material.dart';
import 'package:health/health.dart';
class SimpleHealthExample extends StatefulWidget {
@override
_SimpleHealthExampleState createState() => _SimpleHealthExampleState();
}
class _SimpleHealthExampleState extends State<SimpleHealthExample> {
final health = Health();
int? steps;
@override
void initState() {
super.initState();
_initHealth();
}
Future<void> _initHealth() async {
// Configure
await health.configure();
// Request permissions
final types = [HealthDataType.STEPS];
final granted = await health.requestAuthorization(types);
if (granted) {
_fetchSteps();
}
}
Future<void> _fetchSteps() async {
final now = DateTime.now();
final midnight = DateTime(now.year, now.month, now.day);
final stepCount = await health.getTotalStepsInInterval(midnight, now);
setState(() {
steps = stepCount;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Health Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Steps Today',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 16),
Text(
'${steps ?? '...'}',
style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold),
),
SizedBox(height: 24),
ElevatedButton(
onPressed: _fetchSteps,
child: Text('Refresh'),
),
],
),
),
);
}
}
Permission Denied on iOS

On iOS, if permissions are denied once, they can only be changed in the Settings app. Guide users to:

  1. Open Settings app
  2. Scroll to your app
  3. Tap Health
  4. Enable the data types you need
Health Connect Not Installed (Android)

Use the installation helper:

if (!await health.isHealthConnectAvailable()) {
await health.installHealthConnect();
}
No Data Returned

Common causes:

  • Permissions not granted
  • No data exists for the time range
  • On iOS: device is locked
  • On Android: Health Connect not synced