Result<O, E> class abstract

A Result is an object which can hold two possible values, an Ok or an Err.

Example:

Result<String, Exception> getData() {
  return Ok('value');
}
void main() {
  final result = getData();
  result.fold(
    (ok) => print(ok),
    (err) => print(err),
  );
}

Example 2: using Result inside of a Cubit

class MyCubit extends Cubit<MyState> {
 MyCubit(): super(MyInitialState());

  void login(String username, String password) async {
    final result = await myRepository.login(username, password);
    result.fold(
      (ok) => emit(MyLoggedInState(ok));
      (err) => emit(MyFailedLoginState(err));
    );
  }
}
Implementers

Constructors

Result()
const
Result.ofSync(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.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isErr bool
returns whether the result is Err without exposing the value.
no setter
isOk bool
returns whether the result is Ok without exposing the value.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

err() → E?
Returns the Err value as nullable.
fold<B>(B ok(O ok), B err(E err)) → B
Breaks the result into Ok or Err.
map<A, B>({A ok(O ok)?, B err(E err)?}) Result<A, B>
Take the current Result and transform it's values to a new Result.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
ok() → O?
Returns the Ok value as nullable.
toString() String
A string representation of this object.
override
unwrap([dynamic reason]) → O
Unwrap the result to get the Ok value.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

of<O, E>(FutureOr<O> function()) Future<Result<O, E>>
Creates a Result from an asynchronous callback Can be used to simplify the creation of a result when the error type is known.