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