requestHealthData static method
Requests read access to health types and returns the outcome.
Health data is not one permission: Apple HealthKit and Android Health Connect authorise each HealthDataType separately, so the caller has to say which ones the study reads. An empty list resolves to RPPermissionStatus.unsupported — there is nothing to ask for.
Nothing is requested when access to all of types has already been given,
because requestAuthorization can block in that case.
On iOS the returned status is optimistic. HealthKit does not disclose whether read access was granted — for privacy, an app cannot tell the difference between "no permission" and "no data" — so RPPermissionStatus.granted here means the authorisation sheet was shown without error, not that the participant agreed. Android Health Connect reports the real outcome.
Implementation
static Future<RPPermissionStatus> requestHealthData(
List<HealthDataType> types,
) async {
if (types.isEmpty) return RPPermissionStatus.unsupported;
try {
final health = Health();
// Null on iOS - undetermined, not denied.
if (await health.hasPermissions(types) ?? false) {
return RPPermissionStatus.granted;
}
return await health.requestAuthorization(types)
? RPPermissionStatus.granted
: RPPermissionStatus.denied;
} catch (error) {
debugPrint('$RPPermissions - error requesting health data - $error');
return RPPermissionStatus.unknown;
}
}