downloadFile method

  1. @override
Future<void> downloadFile({
  1. required String containerId,
  2. required String cloudRelativePath,
  3. required String localPath,
  4. StreamHandler<ICloudTransferProgress>? onProgress,
})
override

Download a file from iCloud, then copy it out to a local path.

containerId is the iCloud Container Id.

cloudRelativePath is the relative path of the file in the container. localPath is the full path where the local copy should be written.

Trailing slashes are rejected here because transfers are file-centric and coordinated through UIDocument/NSDocument (directories are not supported).

onProgress is an optional callback to track the progress of the download. It receives a Stream<ICloudTransferProgress> that emits:

The returned future completes once the copy-out finishes (not when iCloud completes any background sync). This is not in-place access.

Implementation

@override
Future<void> downloadFile({
  required String containerId,
  required String cloudRelativePath,
  required String localPath,
  StreamHandler<ICloudTransferProgress>? onProgress,
}) async {
  var eventChannelName = '';

  if (onProgress != null) {
    eventChannelName = _generateEventChannelName('downloadFile', containerId);

    await methodChannel.invokeMethod(
      'createEventChannel',
      {'eventChannelName': eventChannelName},
    );

    final downloadEventChannel = EventChannel(eventChannelName);
    final stream = _receiveTransferProgressStream(downloadEventChannel);

    onProgress(stream);
  }

  await methodChannel.invokeMethod('downloadFile', {
    'containerId': containerId,
    'cloudRelativePath': cloudRelativePath,
    'localFilePath': localPath,
    'eventChannelName': eventChannelName,
  });
}