Flutter 未处理的异常:在Flatter中下载文件时,在反按dispose()后调用setState()

Flutter 未处理的异常:在Flatter中下载文件时,在反按dispose()后调用setState(),flutter,dio,Flutter,Dio,我正在使用dio插件在flatter中下载文件,并在widgets中显示下载进度,同时从当前下载屏幕进行反压以获得以下异常 Unhandled Exception: setState() called after dispose(): _FileDownloadCardState#537ff(lifecycle state: defunct, not mounted) 这是我的代码,带有一些文件验证 Future<void> downloadFile(OfflineMapFile

我正在使用
dio
插件在flatter中下载文件,并在widgets中显示下载进度,同时从当前下载屏幕进行反压以获得以下异常

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");
    }
  }
未来下载文件(OfflineMapFile OfflineMapFile)异步{
if(offlineMapFile.isDownloading){
返回;
}
目录fileDir;
试一试{
offlineMapFile.isDownload=true;
Dio Dio=Dio();
Directory dir=await UAAppContext.getInstance().appDir;
//todo:需要在ios上进行测试
fileDir=等待新目录(dir.path+offlineMapFile.fileStoragePath).create();
等待下载(
offlineMapFile.fileUrl,“${fileDir.path}/${offlineMapFile.fileName}”,
收到进度:(已收到,总计){
如果(总计!=-1){
打印(“记录:$Rec:$received,Total:$Total”);
debugPrint(“目录路径:”+
offlineMapFile.files存储路径+
"/" +
offlineMapFile.fileName);
设置状态(){
offlineMapFile.isDownload=true;
offlineMapFile.isDownloaded=false;
downloadProgressString=(已接收/总计)*100.toStringAsFixed(0)+“%”;
});
}
});
}捕获(e){
印刷品(e);
设置状态(){
offlineMapFile.isDownload=false;
offlineMapFile.isDownloaded=false;});
}最后{
//TODO检查文件是否已下载!验证
//并相应地设置状态。
设置状态(){
//TODO根据文件验证更改布尔值
offlineMapFile.isDownload=false;
offlineMapFile.isDownloaded=true;
downloadProgressString=“已完成”;
if(offlineMapFile.fileName.contains(“.zip”)&&fileDir!=null){
CommonUtils.unzipFile(fileDir.path+“/”+offlineMapFile.fileName,fileDir.path+“/Offline/”;
}
});
打印(“下载完成”);
}
}

您正在对未安装的小部件调用
setState
,要解决此问题,只需添加此条件

if(mounted) {
  setState(//....
}

我们是否有任何东西可以通过另一个屏幕导航来保持文件下载进度?假设这是一个下载屏幕,在这里显示下载进度,当文件下载正在进行时,我移动到另一个屏幕,来到同一个下载屏幕,但无法保持当前的下载进度@sunku7