downloadFile method
Future<void>
downloadFile({
- required String containerId,
- required String cloudRelativePath,
- required String localPath,
- 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:
- progress events with ICloudTransferProgress.percent
- terminal
doneevents - terminal
errorevents (data events, not streamonError)
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,
});
}