フラッターでファイルをダウンロードし、ウィジェットでダウンロードの進行状況を表示するためにプラグインを使用していdio
ますが、現在のダウンロード画面からバックプレスを実行すると例外が発生します
Unhandled Exception: setState() called after dispose(): _FileDownloadCardState#537ff(lifecycle state: defunct, not mounted)
これは、いくつかのファイル検証を含む私のコードです
Future<void> downloadFile(OfflineMapFile offlineMapFile) async {
if(offlineMapFile.isDownloading) {
return;
}
Directory fileDir;
try {
offlineMapFile.isDownloading = true;
Dio dio = Dio();
Directory dir = await UAAppContext.getInstance().appDir;
//todo: need to test on ios
fileDir = await new Directory(dir.path + offlineMapFile.fileStoragePath).create();
await dio.download(
offlineMapFile.fileUrl, "${fileDir.path}/${offlineMapFile.fileName}",
onReceiveProgress: (received, total) {
if (total != -1) {
print("Rec: $received , Total: $total");
debugPrint("Directory path : " +
offlineMapFile.fileStoragePath +
"/" +
offlineMapFile.fileName);
setState(() {
offlineMapFile.isDownloading = true;
offlineMapFile.isDownloaded = false;
downloadProgressString = ((received / total) * 100).toStringAsFixed(0) + "%";
});
}
});
} catch (e) {
print(e);
setState(() {
offlineMapFile.isDownloading = false;
offlineMapFile.isDownloaded = false;});
} finally {
// TODO Check whether the files are downloaded or not! Validation
// And set state accordingly.
setState(() {
//TODO change boolean as per the file Validation
offlineMapFile.isDownloading = false;
offlineMapFile.isDownloaded = true;
downloadProgressString = "Completed";
if(offlineMapFile.fileName.contains(".zip") && fileDir != null){
CommonUtils.unzipFile(fileDir.path +"/"+ offlineMapFile.fileName, fileDir.path + "/Offline/");
}
});
print("Download completed");
}
}