download method
void
download(
{ - String? url,
- File? file,
- void onReceiveProgress(
- int received,
- int total,
- bool failed
)?,
- void onDone()?,
})
override
Implementation
@override
void download({
String? url,
File? file,
void Function(int received, int total, bool failed)? onReceiveProgress,
void Function()? onDone,
}) async {
if (status != UpgradeStatus.available) {
debugPrint("[UpgradeManager] The update is not available.");
return;
}
final uri = Uri.parse(url ?? fileURL);
state.updateUpgradeStatus(status: UpgradeStatus.downloading);
final StreamedResponse response = await Client().send(Request('GET', uri));
final contentLength = response.contentLength ?? 1;
List<int> buffer = [];
response.stream.listen((bytes) async {
buffer.addAll(bytes);
onReceiveProgress?.call(buffer.length, contentLength, false);
},
onDone: () async {
file ??= await defaultFileLocation(uri.pathSegments.last);
await file!.writeAsBytes(buffer);
filePath = file!.absolute.path;
state.updateUpgradeStatus(status: UpgradeStatus.readyToInstall);
onReceiveProgress?.call(contentLength, contentLength, false);
onDone?.call();
},
onError: (e) {
onReceiveProgress?.call(buffer.length, contentLength, true);
state.updateUpgradeStatus(status: UpgradeStatus.error);
debugPrint("[UpgradeManager] Downloading Error: ${e.toString()}");
},
cancelOnError: true,
);
}