簡體   English   中英

flutter_downloader 如果文件存在繼續下載下一個文件

[英]flutter_downloader if file exist continue to download next file

我從 xml 播放列表下載了一個文件,但我想如果該文件存在 flutter_downloader 繼續下一個文件下載,我該怎么做?

在這種情況下,僅下載並檢查播放列表中的第一個文件

    /// check if have permissions
    if(status.isGranted){
      /// get playlist of the day
      for(var item in lista){
        if(item['data'] == formattedDate){
          var brani = item['lista']['brano'];
          /// get the songs from the playlist
          for(var brano in brani){
            var db = await openDatabase('download_tasks.db');
            var dbRecord = '${brano['md5']}.mp3';
            /// check if task table exist
            var checkTable = await db.rawQuery("SELECT COUNT(*) as tabs FROM sqlite_master WHERE type='table' AND name='task'");
            var numTabs = checkTable.first['tabs'] as int;
            /// if table exist download the songs and check if the songs exist
            if(numTabs > 0){
              var oldFile = await db.rawQuery("SELECT file_name FROM task WHERE file_name='"+dbRecord+"' AND progress = '100';");
              List list = await oldFile;
              for (var item in list){
                if(item['file_name'] == dbRecord){
                  print('il file ${dbRecord} esiste');
                    } else {
                      print('il file ${dbRecord} NON esiste');
                      await downloadBrani(brano['nome'], brano['md5']);
                    }
                  }
            } else {
              await downloadBrani(brano['nome'], brano['md5']);
            }
          }
        }
      }
    }else{
      print('permission denied');
    }

太感謝了

我這里有一個例子,你可以嘗試將你的代碼替換到相應的區域

  var files_local = ['ghi']; // local file names, replace query here
  var files_to_download = ['abc', 'def', 'ghi']; // your file names need to download, replace your files list here
  var permission_granted = true; // permission here
  
  if(permission_granted){
    var downloader = Future.wait(
      files_to_download
        .map((file) async {
          // Check file exits
          if(files_local.contains(file)) return 'file exits, skip!';
          // Function that handle file download here
          return 'file name not exits, downloading...';
        }),
    );
    
    // start to download
    downloader.then((results) {
      // all done with `results` is result futures
      for(var result in results) print("result: $result");
    });
  }else{
    print('permission denied');
  }

// ----- result ------
result: file name not exits, downloading...
result: file name not exits, downloading...
result: file exits, skip!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM