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,
  9. bool deleteOnError = false,
})
override

Downloads an APK file and invokes the native method channel to install it.

This method delegates the downloading process to ApiService. If the download is successful and a valid file path is returned, it invokes the downloadAndInstallApk method on the native platform channel.

Parameters:

  • downloadFileUrl: The direct URL to the .apk file. Must not be empty.
  • onProgress: Callback returning the download progress as a double (0.0 to 1.0).
  • onError: (Deprecated) Callback for legacy error handling. Use try-catch instead.
  • onTimeLeft: Callback returning a formatted string of the estimated time remaining.
  • onSpeed: Callback returning a formatted string of the current download speed.
  • onTotalSize: Callback returning a formatted string of the total file size.
  • onDownloadedSize: Callback returning a formatted string of the bytes downloaded so far.
  • downloadFileName: Optional custom name for the downloaded file (without the .apk extension).
  • deleteOnError: If true, automatically deletes the incomplete file if the download fails. Defaults to false.

Throws:

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,
  bool deleteOnError = false,
}) 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,
      deleteOnError: deleteOnError,
    );

    // 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);
  }
}