0

i download a file from a xml playlist, but i want if the file exist flutter_downloader continue with the next file download, how can i do this ?

In this case download and check only the first file in playlist

    /// 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');
    }

Thank you so much

amepro
  • 43
  • 7
  • Why you looping list of old file? u can only check the name has exits once. And when you need do many call in 1 time, i recomand using `Future.await` instead of for loop. – Tuan Apr 07 '22 at 08:44
  • i looping on list of old file because the rawQuery give me an array of items, exist a method to return a bool response from rawQuery ? – amepro Apr 07 '22 at 08:48
  • I dont know about `rawQuery`, but from a List of item as result you can use `contains` or `any` to check exits of file name, eg: bool exits = list.contains(dbRecord) – Tuan Apr 07 '22 at 08:58
  • whit your solution i get always FALSE with the loop the real results I/flutter (10649): this is oldFile [{file_name: xxxxxx.mp3}] I/flutter (10649): this check if list contains dbRecord: false I/flutter (10649): the file xxxxxx.mp3 exists – amepro Apr 07 '22 at 09:12

1 Answers1

0

I have an example here, You can try replace your code to corresponding area

  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!
Tuan
  • 2,033
  • 1
  • 9
  • 16