downloadFile method

  1. @override
Future<DownloadFileResult> downloadFile({
  1. required DownloadFileRequest request,
  2. void onProgress(
    1. TransferProgress
    )?,
})

Implementation

@override
Future<DownloadFileResult> downloadFile(
    {required DownloadFileRequest request,
    void Function(TransferProgress)? onProgress}) async {
  // Download to a non-existent, empty file as required by iOS, then move
  // the file into its correct position as determined by `request`.
  final tempDir = await getTemporaryDirectory();
  final tempFile = File(
    path.join(tempDir.path, 'amplify_temp_${request.uuid}'),
  );
  try {
    if (onProgress != null) {
      _transferProgressionCallbackMap[request.uuid] = onProgress;
    }
    final Map<String, dynamic>? data =
        (await _channel.invokeMapMethod<String, dynamic>(
      'downloadFile',
      request.copyWith(local: tempFile).serializeAsMap(),
    ));
    if (data == null) {
      throw AmplifyException(
          AmplifyExceptionMessages.nullReturnedFromMethodChannel);
    }
    if (await request.local.exists()) {
      await request.local.delete();
    }
    return DownloadFileResult(
      file: await tempFile.copy(request.local.path),
    );
  } on PlatformException catch (e) {
    throw _convertToStorageException(e);
  } finally {
    _transferProgressionCallbackMap.remove(request.uuid);
    tempFile.delete().ignore();
  }
}