Home > Enterprise >  Dart how to call the same function when it ends*?
Dart how to call the same function when it ends*?

Time:09-21

I have a list of Files that need to be uploaded to a server. The upload function has a callback that indicates when the upload is complete. How can I upload each file in a list sequentially until all files have been uploaded?

//My file list: 
List<File> _list;
//The upload function accepts a single File and has an onComplete function
upload(_list.first, (){
   //Now I need to trigger an upload of the second file in the list and so on until all tasks have been uploaded. 
});

How can I get the files to upload sequentially?

Thank you for your help.

CodePudding user response:

How about:

void uploadAll(List<File> files) {
  int cursor = 0;
  void uploadNext() {
    if (cursor == values.length) return;
    upload(values[cursor  ], uploadNext);
  }
  uploadNext();
}
  • Related