From fa7d8683c155dc779c8702e4c7669c2fb1ff4c8a Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 24 Nov 2026 15:37:36 +0100 Subject: [PATCH] Detect missing HealthKit authorization by its error code --- .../health/providers/health_repository.dart | 26 ++++++++++-- .../providers/health_repository_test.dart | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/lib/features/health/providers/health_repository.dart b/lib/features/health/providers/health_repository.dart index d9af82882..7d02f07df 100644 --- a/lib/features/health/providers/health_repository.dart +++ b/lib/features/health/providers/health_repository.dart @@ -141,9 +141,29 @@ class HealthRepository { /// does report (a plain denial silently reads as "no data"). Reinstalling /// the app resets the grants while the app's own preferences survive, so a /// sync can hit this even though the user did grant access earlier. - static bool isAuthorizationMissing(Object error) => - error is PlatformException && - (error.message ?? '').toLowerCase().contains('authorization not determined'); + /// + /// Matched by HealthKit's own error code, which the plugin passes on in the + /// details: the message is the localized description, so it only reads + /// "authorization not determined" on an English device. The plugin's + /// `PERMISSION_DENIED` is no substitute, it also covers restrictions (Screen + /// Time, managed devices, guest mode) that asking again cannot lift. The + /// message stays as a fallback for an error without the details. + static bool isAuthorizationMissing(Object error) { + if (error is! PlatformException) { + return false; + } + final details = error.details; + if (details is Map && details['domain'] == _healthKitErrorDomain) { + return details['nativeCode'] == _healthKitAuthorizationNotDetermined; + } + return (error.message ?? '').toLowerCase().contains('authorization not determined'); + } + + /// `HKErrorDomain` + static const _healthKitErrorDomain = 'com.apple.healthkit'; + + /// `HKError.Code.errorAuthorizationNotDetermined` + static const _healthKitAuthorizationNotDetermined = 5; /// Ensures READ access to [types] (requesting it if needed) and, on Android, /// access to historical data. Returns whether access is granted. diff --git a/test/features/health/providers/health_repository_test.dart b/test/features/health/providers/health_repository_test.dart index d2ba5bc5b..d290075a5 100644 --- a/test/features/health/providers/health_repository_test.dart +++ b/test/features/health/providers/health_repository_test.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'package:flutter/services.dart' show PlatformException; import 'package:flutter_test/flutter_test.dart'; import 'package:health/health.dart'; import 'package:mockito/annotations.dart'; @@ -398,4 +399,43 @@ void main() { expect(readable, {HealthDataType.WEIGHT}); }); }); + + group('isAuthorizationMissing', () { + PlatformException healthKitError(int nativeCode, String message) => PlatformException( + code: 'PERMISSION_DENIED', + message: 'Error getting health data: $message', + details: {'domain': 'com.apple.healthkit', 'nativeCode': nativeCode}, + ); + + test('is recognized by its HealthKit code on a non-English device', () { + // The message is HealthKit's localized description + expect( + HealthRepository.isAuthorizationMissing( + healthKitError(5, 'Die Autorisierung ist nicht festgelegt'), + ), + isTrue, + ); + }); + + test('a restriction is not missing authorization', () { + // Screen Time or a managed device: asking again cannot lift it, even + // though the plugin reports it with the same PERMISSION_DENIED + expect( + HealthRepository.isAuthorizationMissing(healthKitError(2, 'Health data is restricted')), + isFalse, + ); + }); + + test('falls back to the message without HealthKit details', () { + expect( + HealthRepository.isAuthorizationMissing( + PlatformException( + code: 'HEALTH_ERROR', + message: 'Error getting health data: Authorization not determined', + ), + ), + isTrue, + ); + }); + }); }