Skip to content

[health] Unable to fetch SLEEP_ASLEEP and SLEEP_IN_BED data on iOS, while SLEEP_AWAKE is successful #3

@ScottFarestrand

Description

@ScottFarestrand

Plugin Name

health

Plugin Version

I tried health: ^13.2.0 and health: 12.0.0

Device

IPone 13 PRO

Operating System

IOS 18.6.2

Describe the bug

Hello,

I am experiencing an issue on iOS where I am unable to fetch HealthDataType.SLEEP_ASLEEP and HealthDataType.SLEEP_IN_BED data, even though all permissions appear to be correctly configured and granted. Interestingly, HealthDataType.SLEEP_AWAKE data is fetched successfully from the same sleep session.

Steps Taken:

Project Setup: The HealthKit capability is enabled in Xcode, and the NSHealthShareUsageDescription key is present in Info.plist.

Permissions: I am requesting authorization for all three sleep types (SLEEP_IN_BED, SLEEP_ASLEEP, SLEEP_AWAKE). In the iOS Settings app (Settings > Health > Data Access & Devices), the master "Sleep" toggle for my app is enabled.

Data Confirmation: I have manually verified in the Apple Health app that a complete sleep session, including "Time in Bed" and "Time Asleep" data, exists for the time range being queried.

Code: The query is being made using health.getHealthDataFromTypes(...).

Debugging: We have tried numerous debugging steps, including a full permission reset (uninstalling the app, restarting the device, reinstalling), cleaning the Xcode build folder, and adjusting the query parameters.

Observed Behavior:

The call to health.getHealthDataFromTypes consistently returns a list containing only the SLEEP_AWAKE data points. The SLEEP_ASLEEP and SLEEP_IN_BED data points from the same night are always missing.

This suggests an issue with how the package is fetching or being granted permission for these specific data types on certain iOS versions, despite the user granting full "Sleep" access.

Environment:

health package version:(health: ^13.2.0). I also tried health: 12.0.0

Thank you for looking into this.

Steps to Reproduce

Here is code from my health_service.dart file final FirebaseAuth _auth = FirebaseAuth.instance;

static final types = [
HealthDataType.SLEEP_IN_BED,
HealthDataType.SLEEP_ASLEEP,
HealthDataType.SLEEP_AWAKE,
HealthDataType.WORKOUT,
// HealthDataType.HEART_RATE,
];

Future fetchAndSaveHealthData() async {
final userId = _auth.currentUser?.uid;
if (userId == null) return;

print("--- Health Sync Started: Final Reset Test ---");

bool isAuthorized = await _health.requestAuthorization(types);
if (!isAuthorized) {
  print("Authorization was denied.");
  return;
}

print("Permissions granted. Fetching data...");

final now = DateTime.now();
final startTime = now.subtract(const Duration(hours: 48));

try {
  final healthData = await _health.getHealthDataFromTypes(
    startTime: startTime,
    endTime: now,
    types: types,
  );

  final uniqueData = healthData.toSet().toList();

  if (uniqueData.isEmpty) {
    print('No health data was returned from the API.');
    return;
  }

  print("\n--- Raw Data From API (${uniqueData.length} points) ---");
  for (final point in uniqueData) {
    final value = (point.value is NumericHealthValue)
        ? (point.value as NumericHealthValue).numericValue.toString()
        : point.dateTo.difference(point.dateFrom).inMinutes.toString() +
              ' mins';
    print(" -> ${point.type.name}: $value");
  }
  print("--------------------------------\n");

  // ... your saving logic ...
} catch (e) {
  print("Error during health data sync: $e");
}
print("--- Health Sync Finished ---");

}
}

Expected Behavior

I expected to get data for SLEEP_ASLEEP, SLEEP_IN_BED, and SLEEP_AWAKE

Actual Behavior

lutter: Permissions granted. Fetching data...
4
flutter: SLEEP_AWAKE
flutter:
--- Raw Data From API (11 points) ---
flutter: -> SLEEP_AWAKE: 4.0
flutter: -> SLEEP_AWAKE: 0.5
flutter: -> SLEEP_AWAKE: 1.0
flutter: -> SLEEP_AWAKE: 10.0
flutter: -> SLEEP_AWAKE: 1.0
flutter: -> SLEEP_AWAKE: 0.5
flutter: -> SLEEP_AWAKE: 1.5
flutter: -> SLEEP_AWAKE: 6.5
flutter: -> SLEEP_AWAKE: 1.0
flutter: -> WORKOUT: 95 mins
flutter: -> WORKOUT: 162 mins
flutter: --------------------------------
flutter: --- Health Sync Finished ---
4
flutter: SLEEP_AWAKE

Flutter Logs

Screenshots

No response

Flutter Doctor Output

Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, 3.35.4, on macOS 15.6.1 24G90 darwin-arm64, locale en-US)
    ! Warning: `flutter` on your path resolves to /Users/scottfarestrand/Development/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at /Users/scottfarestrand/development/flutter. Consider adding
      /Users/scottfarestrand/development/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/scottfarestrand/Development/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/scottfarestrand/development/flutter. Consider adding
      /Users/scottfarestrand/development/flutter/bin to the front of your path.
[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 26.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.104.1)
[✓] Connected device (6 available)
    ! Error: Browsing on the local area network for LeeAnn’s iPhone. Ensure the device is unlocked and attached with a cable
      or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions