downloadAndInstallApk method
Future<void>
downloadAndInstallApk({
- required String downloadFileUrl,
- void onProgress(
- double progress
- @Deprecated('Use try-catch with FileDownloadException instead.') void onError(
- String error
- void onTimeLeft(
- String error
- void onSpeed(
- String error
- void onTotalSize(
- String error
- void onDownloadedSize(
- String error
- String? downloadFileName,
- 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.apkfile. 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.apkextension).deleteOnError: Iftrue, automatically deletes the incomplete file if the download fails. Defaults tofalse.
Throws:
- ArgumentError if the
downloadFileUrlis empty. - FileDownloadException for network, storage, or platform-specific errors.
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);
}
}