Okay, so I have a List of Files and I need to run a function on each member of the list. I essentially want to do something like this:
for(File file in files) {
functionThatReturnsAFuture(file);
}
But obviously this won't work, since the function that returns a Future fires of asynchronously. Is my only option something like this?
List<File> files = new List<File>();
// Add files somewhere
Future processFile(int i) {
return new Future.sync(() {
//Do stuff to the file
if(files.length>i+1) {
return processFile(i+1);
}
});
}
processFile(0);
EDIT: I suppose more context is important. The end goal is to combine several files into a single JSON object for submitting to a server. The FileReader object uses events to perform a read, so I used a Completer to create a wrapper function that provides a Future. I could let all of these run asynchronously, and then fire off an event that submits it when they are all done, but that's comparatively a lot of setup vs. a for-each-loop that makes it all work (if one exists, anyway). The core issue is needing to run a Future-returning function on a List of files and then perform an action that depends on them all having completed.