DART: How to write timeconsuming function returning a Future

Viewed 180

I have to write a timeconsuming function that will return a Future, if it´s ready. Is the approach below correct, or does my timeconsuming algorithm in line 9 block the program til it´s ready. In this case, what would I have to do, to give control back to the eventloop or what else could be a solution?

Future<int> timeconsumingFunctionReturningFuture(int i) {
  var completer = new Completer();
 
  if (i==0) { 
    completer.completeError(88);
    return completer.future;
  } else {
    int rc;
    // Line9: rc = timeconsuming algorithm, to calculate rc
    completer.complete(rc);
    return completer.future;
  }
}
2 Answers
Related