downloadAndInstallApk method

  1. @override
Future<void> downloadAndInstallApk({
  1. required String downloadFileUrl,
  2. void onProgress(
    1. double progress
    )?,
  3. @Deprecated('Use try-catch with FileDownloadException instead.') void onError(
    1. String error
    )?,
  4. void onTimeLeft(
    1. String error
    )?,
  5. void onSpeed(
    1. String error
    )?,
  6. void onTotalSize(
    1. String error
    )?,
  7. void onDownloadedSize(
    1. String error
    )?,
  8. String? downloadFileName,
})
override

Implementation

@override
Future<void> downloadAndInstallApk({
  required String downloadFileUrl,
  void Function(double progress)? onProgress,
  @Deprecated('Use try-catch with FileDownloadException instead.')
  void Function(String error)? onError,
  void Function(String timeLeft)? onTimeLeft,
  void Function(String speed)? onSpeed,
  void Function(String totalSize)? onTotalSize,
  void Function(String downloadedSize)? onDownloadedSize,
  String? downloadFileName,
}) async {
  try {
    if (downloadFileUrl.trim().isEmpty) {
      throw ArgumentError("Download File Url cannot be empty");
    }

    // 1. Await ApiService directly. Do NOT pass onSuccess/onError here anymore.
    // Let ApiService throw the Exception naturally.
    String? filePath = await ApiService().downloadFile(
      downloadFileUrl: downloadFileUrl,
      downloadFileName: downloadFileName,
      onProgress: onProgress,
      onTimeLeft: onTimeLeft,
      onSpeed: onSpeed,
      onTotalSize: onTotalSize,
      onDownloadedSize: onDownloadedSize,
    );

    // 2. If successful, trigger the native installation
    if (filePath != null) {
      await _channel
          .invokeMethod<String>('downloadAndInstallApk', {"path": filePath});
    }
  } on FileDownloadException catch (e, sc) {
    printLog(e.toString(), stackTrace: sc);
    _handleErrorBridge(e, "Error occurred during download", onError);
  } on PlatformException catch (e, sc) {
    printLog(e.toString(), stackTrace: sc);
    final exception = FileDownloadException(
        type: DownloadErrorType.unknown, originalError: e);
    _handleErrorBridge(
        exception, e.message ?? "Platform Exception occurred", onError);
  } catch (e, sc) {
    printLog(e.toString(), stackTrace: sc);
    final exception = FileDownloadException(
        type: DownloadErrorType.unknown, originalError: e);
    _handleErrorBridge(exception, e.toString(), onError);
  }
}