writeAudiogram method

Future<bool> writeAudiogram(
  1. List<double> frequencies,
  2. List<double> leftEarSensitivities,
  3. List<double> rightEarSensitivities,
  4. DateTime startTime,
  5. DateTime endTime, {
  6. Map<String, dynamic>? metadata,
})

Saves audiogram into Apple Health.

Returns true if successful, false otherwise.

Parameters:

  • frequencies - array of frequencies of the test
  • leftEarSensitivities threshold in decibel for the left ear
  • rightEarSensitivities threshold in decibel for the left ear
  • startTime - the start time when the audiogram is measured.
    • It must be equal to or earlier than endTime.
  • endTime - the end time when the audiogram is measured.
    • It must be equal to or later than startTime.
    • Simply set endTime equal to startTime if the audiogram is measured only at a specific point in time.
  • metadata - optional map of keys, both HKMetadataKeyExternalUUID and HKMetadataKeyDeviceName are required

Implementation

Future<bool> writeAudiogram(
    List<double> frequencies,
    List<double> leftEarSensitivities,
    List<double> rightEarSensitivities,
    DateTime startTime,
    DateTime endTime,
    {Map<String, dynamic>? metadata}) async {
  if (frequencies.isEmpty ||
      leftEarSensitivities.isEmpty ||
      rightEarSensitivities.isEmpty)
    throw ArgumentError(
        "frequencies, leftEarSensitivities and rightEarSensitivities can't be empty");
  if (frequencies.length != leftEarSensitivities.length ||
      rightEarSensitivities.length != leftEarSensitivities.length)
    throw ArgumentError(
        "frequencies, leftEarSensitivities and rightEarSensitivities need to be of the same length");
  if (startTime.isAfter(endTime))
    throw ArgumentError("startTime must be equal or earlier than endTime");
  if (_platformType == PlatformType.ANDROID)
    throw UnsupportedError("writeAudiogram is not supported on Android");
  Map<String, dynamic> args = {
    'frequencies': frequencies,
    'leftEarSensitivities': leftEarSensitivities,
    'rightEarSensitivities': rightEarSensitivities,
    'dataTypeKey': HealthDataType.AUDIOGRAM.typeToString(),
    'startTime': startTime.millisecondsSinceEpoch,
    'endTime': endTime.millisecondsSinceEpoch,
    'metadata': metadata,
  };
  bool? success = await _channel.invokeMethod('writeAudiogram', args);
  return success ?? false;
}