toResult method

Future<Result<T>> toResult()

Catches any unhandled exceptions during the execution lifecycle of the host Future and automatically maps the outcome into a type-safe Result object.

This eliminates the necessity of writing local try-catch wrappers for simple async extractions.

// Instead of wrapping inside Result.guardAsync manually:
final Result<User> result = await apiRepository.fetchUserData().toResult();

Implementation

Future<Result<T>> toResult() async {
  try {
    final data = await this;
    return Result.success(data);
  } catch (e, stackTrace) {
    return Result.failure(
      Failure(message: e.toString(), error: e, stackTrace: stackTrace),
    );
  }
}