Future.sync(dynamic computation())

Creates a future containing the result of immediately calling computation.

If calling computation throws, the returned future is completed with the error.

If calling computation returns a Future, completion of the created future will wait until the returned future completes, and will then complete with the same result.

If calling computation returns a non-future value, the returned future is completed with that value.

Source

factory Future.sync(computation()) {
  try {
    var result = computation();
    return new Future<T>.value(result);
  } catch (error, stackTrace) {
    return new Future<T>.error(error, stackTrace);
  }
}