downloadUpdateWindows static method
Future<void>
downloadUpdateWindows(
- String url,
- String versionName,
- dynamic onProgress(),
- UpdateCenterConfig config,
- DownloadState downloadState,
Same as Android only for Windows
Implementation
static Future<void> downloadUpdateWindows(
String url,
String versionName,
Function(double) onProgress,
UpdateCenterConfig config,
DownloadState downloadState,
) async {
downloadState.isDownloading.value = true;
var response = await h.Client().send(h.Request('GET', Uri.parse(url)));
Directory tempDirectory = await MemoryProvider.directoryWindows();
String fileName = '${tempDirectory.path}/${url.split('/').last}';
final contentLength = response.contentLength ?? 0;
int bytesDownloaded = 0;
double lastNotifiedProgress = 0.0;
final file = await MemoryProvider.getLocalFileWindows(url);
var fileStream = file.openWrite();
response.stream.listen(
(List<int> newBytes) {
bytesDownloaded += newBytes.length;
fileStream.add(newBytes);
// Calculate progress
double currentProgress = bytesDownloaded / contentLength;
onProgress(currentProgress);
// Inside your download logic
downloadState.progress.value = currentProgress; // currentProgress is a value between 0.0 and 1.0
downloadState.progressText.value = "${formatBytes(bytesDownloaded, 2)}/${formatBytes(contentLength, 2)}";
// Throttle the notification update
if (currentProgress - lastNotifiedProgress >= 0.02 || currentProgress == 1.0) {
lastNotifiedProgress = currentProgress;
}
},
onDone: () async {
await fileStream.flush();
await fileStream.close();
await OpenFilex.open(fileName);
downloadState.isDownloading.value = false;
},
onError: (e) async {
await fileStream.close();
downloadState.isDownloading.value = false;
log(e);
},
cancelOnError: true,
);
}