downloadAsset method

Future<void> downloadAsset({
  1. required CloudKitRecordId id,
  2. required String field,
  3. required File destination,
  4. void onProgress(
    1. double fraction
    )?,
})

Writes the asset in field of the record id to destination, replacing any file there. A destination that is a directory fails with CloudKitSyncException.invalidArgument. onProgress receives the fraction of the download completed, from 0 to 1.

Implementation

Future<void> downloadAsset({
  required CloudKitRecordId id,
  required String field,
  required File destination,
  void Function(double fraction)? onProgress,
}) async {
  final transferId = onProgress == null
      ? null
      : CloudKitSyncChannel.addProgressListener(onProgress);
  try {
    await CloudKitSyncChannel.invoke<void>('downloadAsset', {
      'containerId': containerId,
      'id': CloudKitChannelCodec.encodeRecordId(id),
      'field': field,
      'destinationPath': destination.path,
      'transferId': transferId,
    });
  } finally {
    if (transferId != null) {
      CloudKitSyncChannel.removeProgressListener(transferId);
    }
  }
}