ValueResult<T> class

A result type that represents either a success value or a failure error.

Use ValueResult.success to create a successful result containing a value. Use ValueResult.failure to create a failed result containing an error message.

Example

final result = await someAsyncOperation();
return result.fold(
  (value) => handleSuccess(value),
  (error) => handleError(error),
);

Usage with Dart's pattern matching

final result = await fetchUser();
if (result.isSuccess) {
  print('User: ${result.value}');
} else {
  print('Error: ${result.error}');
}

Type parameter T is the type of the success value.

Constructors

ValueResult.failure(String error, {String? title})
Creates a failed result containing an error message.
factory
ValueResult.success(T value)
Creates a successful result containing the given value.
factory

Properties

error String
Returns the error message.
no setter
hashCode int
The hash code for this object.
no setteroverride
isError bool
Returns true if this result represents a failure with an error.
no setter
isSuccess bool
Returns true if this result represents a success with a value.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
title String?
Returns the optional error title.
no setter
value → T?
Returns the success value.
no setter

Methods

fold<R>(R onSuccess(T value), R onFailure(String error)) → R
Transforms the result based on its state using pattern matching.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

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

Static Methods

fromError<S>(dynamic exceptionOrError) ValueResult<S>
Creates a failure result from a DioException or any exception object.