Steps
Future<int?> getTotalStepsInInterval( DateTime startDate, DateTime endDate,)Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
startDate |
DateTime |
Yes | Start of time range |
endDate |
DateTime |
Yes | End of time range |
Returns
Section titled “Returns”int: Total steps in the intervalnull: If no step data available or error occurred
Basic Usage
Section titled “Basic Usage”// Get today's stepsfinal todaySteps = await health.getTotalStepsInInterval( DateTime.now().copyWith(hour: 0, minute: 0, second: 0), DateTime.now(),);
print('Steps today: ${todaySteps ?? 0}');
// Get yesterday's stepsfinal yesterday = DateTime.now().subtract(Duration(days: 1));final yesterdaySteps = await health.getTotalStepsInInterval( yesterday.copyWith(hour: 0, minute: 0, second: 0), yesterday.copyWith(hour: 23, minute: 59, second: 59),);
print('Steps yesterday: ${yesterdaySteps ?? 0}');Time Patterns
Section titled “Time Patterns”final now = DateTime.now();final startOfDay = DateTime(now.year, now.month, now.day);final todaySteps = await health.getTotalStepsInInterval(startOfDay, now);This Week
Section titled “This Week”final now = DateTime.now();final startOfWeek = now.subtract(Duration(days: now.weekday - 1));final weekStart = DateTime(startOfWeek.year, startOfWeek.month, startOfWeek.day);final weekSteps = await health.getTotalStepsInInterval(weekStart, now);This Month
Section titled “This Month”final now = DateTime.now();final monthStart = DateTime(now.year, now.month, 1);final monthSteps = await health.getTotalStepsInInterval(monthStart, now);Custom Range
Section titled “Custom Range”final start = DateTime(2024, 1, 1);final end = DateTime(2024, 1, 31);final januarySteps = await health.getTotalStepsInInterval(start, end);print('January total: ${januarySteps ?? 0} steps');Use Cases
Section titled “Use Cases”Step Goal Tracking
Section titled “Step Goal Tracking”class StepGoal { final int dailyGoal;
StepGoal({this.dailyGoal = 10000});
Future<Map<String, dynamic>> getProgress() async { final now = DateTime.now(); final startOfDay = DateTime(now.year, now.month, now.day);
final steps = await health.getTotalStepsInInterval(startOfDay, now); final currentSteps = steps ?? 0; final percentage = (currentSteps / dailyGoal * 100).clamp(0, 100); final remaining = (dailyGoal - currentSteps).clamp(0, dailyGoal);
return { 'current': currentSteps, 'goal': dailyGoal, 'percentage': percentage, 'remaining': remaining, 'achieved': currentSteps >= dailyGoal, }; }}
// Usagefinal goal = StepGoal(dailyGoal: 10000);final progress = await goal.getProgress();print('${progress['current']} / ${progress['goal']} steps');print('${progress['percentage'].toStringAsFixed(1)}% complete');Weekly Step Summary
Section titled “Weekly Step Summary”Future<List<Map<String, dynamic>>> getWeeklySteps() async { final now = DateTime.now(); final weeklySteps = <Map<String, dynamic>>[];
for (int i = 6; i >= 0; i--) { final day = now.subtract(Duration(days: i)); final dayStart = DateTime(day.year, day.month, day.day); final dayEnd = dayStart.add(Duration(days: 1));
final steps = await health.getTotalStepsInInterval(dayStart, dayEnd);
weeklySteps.add({ 'date': dayStart, 'dayName': _getDayName(dayStart.weekday), 'steps': steps ?? 0, }); }
return weeklySteps;}
String _getDayName(int weekday) { const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; return days[weekday - 1];}
// Usagefinal weekData = await getWeeklySteps();for (var day in weekData) { print('${day['dayName']}: ${day['steps']} steps');}Step Streak Tracking
Section titled “Step Streak Tracking”class StepStreak { final int dailyGoal;
StepStreak({this.dailyGoal = 10000});
Future<int> getCurrentStreak() async { int streak = 0; final now = DateTime.now();
for (int i = 0; i < 365; i++) { final day = now.subtract(Duration(days: i)); final dayStart = DateTime(day.year, day.month, day.day); final dayEnd = dayStart.add(Duration(days: 1));
final steps = await health.getTotalStepsInInterval(dayStart, dayEnd);
if (steps != null && steps >= dailyGoal) { streak++; } else { break; } }
return streak; }}
// Usagefinal streakTracker = StepStreak(dailyGoal: 10000);final streak = await streakTracker.getCurrentStreak();print('Current streak: $streak days');Performance Optimization
Section titled “Performance Optimization”This method is optimized for step counting:
// ✅ Efficient - uses optimized step total queryfinal steps = await health.getTotalStepsInInterval(start, end);
// ❌ Less efficient - queries all data points then sumsfinal dataPoints = await health.getHealthDataFromTypes( types: [HealthDataType.STEPS], startDate: start, endDate: end,);final manualTotal = dataPoints .map((e) => (e.value as NumericHealthValue).numericValue) .fold(0.0, (a, b) => a + b);Error Handling
Section titled “Error Handling”try { final steps = await health.getTotalStepsInInterval(start, end);
if (steps == null) { print('No step data available for this period'); } else { print('Total steps: $steps'); }} catch (e) { print('Error fetching steps: $e');}Platform Differences
Section titled “Platform Differences”- Aggregates all step samples from HealthKit
- Includes steps from iPhone, Apple Watch, and connected apps
- Automatic deduplication of overlapping data
- Aggregates step count from Health Connect
- Includes data from all connected apps and devices
- May return
nullif Health Connect is not available
Comparison with Other Methods
Section titled “Comparison with Other Methods”| Method | Use Case | Return Type |
|---|---|---|
getTotalStepsInInterval |
Quick total for a period | int? |
getHealthDataFromTypes |
Individual step samples | List<HealthDataPoint> |
getHealthIntervalDataFromTypes |
Steps bucketed by time | List<HealthDataPoint> |
See Also
Section titled “See Also”- Basic Reading - Read individual step samples
- Interval Reading - Steps grouped by hour/day
- Data Types - Understanding
STEPSdata type