requestHealthPermissions method
Future<bool>
requestHealthPermissions({
- List<
StatisticsType> ? statisticTypes, - List<
MetricType> ? metricTypes, - List<
ActivityConfig> ? activityConfigs, - List<
SleepConfig> ? sleepConfigs, - bool? includeEnhancedPermissions,
- bool? includeBackgroundDelivery,
iOS: This method must be called before requesting any data from HealthKit. If the user has not granted permissions to read HealthKit data, a system-generated dialog will appear listing the required permissions. If the user has already granted or denied permissions, no dialog will be shown. To read data from HealthKit, this should be run after each app start (but it is NOT needed when the app returns from the background).
Android: On Android, calling this method simplifies the use of Health Connect, making it as seamless as using HealthKit. Call it after the app starts; if needed, it will present the user with the OS permission dialog. If you need finer control over the process on android, please use other methods.
Implementation
Future<bool> requestHealthPermissions({
List<StatisticsType>? statisticTypes,
List<MetricType>? metricTypes,
List<ActivityConfig>? activityConfigs,
List<SleepConfig>? sleepConfigs,
bool? includeEnhancedPermissions,
bool? includeBackgroundDelivery,
}) async {
if (Platform.isIOS) {
final result = await NativeSDKBridgeV3.requestPermissionsFromHealthKit(
connectionId: connectionId,
forStatistics: statisticTypes?.map((e) => e.toJson()).toList(),
forMetrics: metricTypes?.map((e) => e.toJson()).toList(),
forActivityConfigs: activityConfigs?.map((e) => jsonEncode(e.toJson())).toList(),
forSleepConfigs: sleepConfigs?.map((e) => jsonEncode(e.toJson())).toList(),
);
ExceptionHandler.checkException(result);
return true;
}
if (Platform.isAndroid) {
await enableHealthConnectIntegration();
final permissions = await NativeSDKBridgeV3.getHealthConnectPermissions(
connectionId: connectionId,
statisticTypes: statisticTypes?.map((e) => e.toJson()).toList() ?? [],
metricTypes: metricTypes?.map((e) => e.toJson()).toList() ?? [],
activityConfigs: activityConfigs?.map((e) => jsonEncode(e.toJson())).toList() ?? [],
sleepConfigs: sleepConfigs?.map((e) => jsonEncode(e.toJson())).toList() ?? [],
includeEnhancedPermissions: includeEnhancedPermissions ?? false,
includeBackgroundDelivery: includeBackgroundDelivery ?? false,
);
// log("Health Connect permissions: $permissions");
final result = await NativeSDKBridgeV3.requestPermissionsFromHealthConnect(
connectionId: connectionId, permissions: permissions);
ExceptionHandler.checkException(result);
return result;
}
throw Exception('requestHealthPermissions method is only available on iOS and Android');
}