download method

  1. @override
Future<void> download({
  1. required String containerId,
  2. required String relativePath,
  3. required String destinationFilePath,
  4. StreamHandler<double>? onProgress,
})
override

Download a file from iCloud.

containerId is the iCloud Container Id.

relativePath is the relative path of the file on iCloud, such as file1 or folder/file2.

destinationFilePath is the full path of the local file to be saved as.

onProgress is an optional callback to track the progress of the download. It takes a Stream

The returned future completes without waiting for the file to be downloaded.

Implementation

@override
Future<void> download({
  required String containerId,
  required String relativePath,
  required String destinationFilePath,
  StreamHandler<double>? onProgress,
}) async {
  var eventChannelName = '';

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

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

    final downloadEventChannel = EventChannel(eventChannelName);
    final stream = downloadEventChannel
        .receiveBroadcastStream()
        .where((event) => event is double)
        .map((event) => event as double);

    onProgress(stream);
  }

  await methodChannel.invokeMethod('download', {
    'containerId': containerId,
    'cloudFileName': relativePath,
    'localFilePath': destinationFilePath,
    'eventChannelName': eventChannelName
  });
}