hasPermissions method

Future<bool?> hasPermissions(
  1. List<HealthDataType> types, {
  2. List<HealthDataAccess>? permissions,
})

Determines if the data types have been granted with the specified access rights.

Returns:

  • true - if all of the data types have been granted with the specfied access rights.
  • false - if any of the data types has not been granted with the specified access right(s)
  • null - if it can not be determined if the data types have been granted with the specified access right(s).

Parameters:

  • types - List of HealthDataType whose permissions are to be checked.
  • permissions - Optional.
    • If unspecified, this method checks if each HealthDataType in types has been granted READ access.
    • If specified, this method checks if each HealthDataType in types has been granted with the access specified in its corresponding entry in this list. The length of this list must be equal to that of types.

Caveat:

As Apple HealthKit will not disclose if READ access has been granted for a data type due to privacy concern, this method can only return null to represent an undertermined status, if it is called on iOS with a READ or READ_WRITE access.

On Android, this function returns true or false, depending on whether the specified access right has been granted.

Implementation

Future<bool?> hasPermissions(List<HealthDataType> types,
    {List<HealthDataAccess>? permissions}) async {
  if (permissions != null && permissions.length != types.length)
    throw ArgumentError(
        "The lists of types and permissions must be of same length.");

  final mTypes = List<HealthDataType>.from(types, growable: true);
  final mPermissions = permissions == null
      ? List<int>.filled(types.length, HealthDataAccess.READ.index,
          growable: true)
      : permissions.map((permission) => permission.index).toList();

  /// On Android, if BMI is requested, then also ask for weight and height
  if (_platformType == PlatformType.ANDROID) _handleBMI(mTypes, mPermissions);

  return await _channel.invokeMethod('hasPermissions', {
    "types": mTypes.map((type) => type.name).toList(),
    "permissions": mPermissions,
  });
}