Result<O, E>.ofSync constructor

Result<O, E>.ofSync(
  1. O function()
)

Creates a Result from a synchronous callback Can be used to simplify the creation of a result when the error type is known.

Catches only the error defined by the type. The type of Err cannot be null. Setting Err type to dynamic catches all errors.

Implementation

factory Result.ofSync(O Function() function) {
  assert(E != Null);
  try {
    final result = function();
    return Ok(result);
    // ignore: nullable_type_in_catch_clause
  } on E catch (e) {
    return Err(e);
  }
}