Result<T, E> class
sealed
The result of an operation.
A Result is either successful (Ok) or erroneous (Err).
Examples
The following function tries to parse an integer. Upon success, the resulting value is wrapped in an Ok. If an error occurs during conversion, an Err is returned.
Result<int, String> tryParse(String source) {
final parsed = int.tryParse(source);
if (parsed != null) {
return Ok(parsed);
}
return Err('not a number: $source');
}
That function can now be used to try and parse an integer, and return with a helpful error in case of failure.
// prints "Ok(2)"
print(tryParse('2'));
// prints "Err(not a number: two)"
print(tryParse('two'));
- Implementers
- Available extensions
- Annotations
-
- @immutable
Constructors
- Result.err(E error)
-
Creates an Err with the given
error.constfactory - Result.ok(T value)
-
Creates an Ok with the given
value.constfactory
Properties
-
err
→ Option<
E> -
The possibly contained error.
no setter
- error → E
-
Available on Result<
Returns the contained error.Never, E> , provided by the ErroneousResult extensionno setter - errorOrNull → E?
-
The contained error if
thisis an Err, ornullotherwise.no setter -
flattened
→ Result<
T, E> -
Available on Result<
Flattens a Result containing another Result.Result< , provided by the FlattenedResult extensionT, E> , E>no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isErr → bool
-
Whether
thisis an Err.no setter - isOk → bool
-
Whether
thisis an Ok.no setter -
iterable
→ Iterable<
T> -
Returns an iterable over the possibly contained value.
no setter
-
ok
→ Option<
T> -
The possibly contained value.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
transposed
→ Option<
Result< T, E> > -
Available on Result<
A Result containing an Option transposed into an Option containing a Result.Option< , provided by the TransposedResult extensionT> , E>no setter - value → T
-
Available on Result<
Returns the contained value.T, Never> , provided by the SuccessfulResult extensionno setter - valueOrNull → T?
-
The contained value if
thisis an Ok, ornullotherwise.no setter
Methods
-
and<
U> (Result< U, E> other) → Result<U, E> -
Returns
otherifthisis an Ok, or an Err of the original error otherwise. -
andThen<
U> (Result< U, E> calculateOther(T value)) → Result<U, E> -
Returns the result of
calculateOtherifthisis an Ok, or an Err of the original error otherwise. -
contains(
T value) → bool -
Returns
trueifthisis an Ok with the givenvalue. -
containsErr(
E error) → bool -
Returns
trueifthisis an Err with the givenerror. -
inspect(
void inspect(T value)) → Result< T, E> -
Calls
inspectwith the contained value ifthisis an Ok. -
inspectErr(
void inspect(E error)) → Result< T, E> -
Calls
inspectwith the contained error ifthisis an Err. -
isErrAnd(
bool condition(E error)) → bool -
Returns
trueifthisis an Err with a contained error that satisfiescondition. -
isOkAnd(
bool condition(T value)) → bool -
Returns
trueifthisis an Ok with a contained value that satisfiescondition. -
map<
U> (U map(T value)) → Result< U, E> -
Transforms the contained value, if any, by applying
mapto it. -
mapErr<
F> (F map(E error)) → Result< T, F> -
Transforms the contained error, if any, by applying
mapto it. -
mapErrOr<
F> (F map(E error), F defaultError) → F -
Returns the contained error, if any, with
mapapplied to it, ordefaultErrorotherwise. -
mapErrOrElse<
F> (F map(E error), F calculateDefaultError(T value)) → F -
Returns the contained error, if any, with
mapapplied to it, or the result ofcalculateDefaultErrorotherwise. -
mapOr<
U> (U map(T value), U defaultValue) → U -
Returns the contained value, if any, with
mapapplied to it, ordefaultValueotherwise. -
mapOrElse<
U> (U map(T value), U calculateDefaultValue(E error)) → U -
Returns the contained value, if any, with
mapapplied to it, or the result ofcalculateDefaultValueotherwise. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
or<
F> (Result< T, F> other) → Result<T, F> -
Returns an Ok of the original value if
thisis an Ok, orotherotherwise. -
orElse<
F> (Result< T, F> calculateOther(E error)) → Result<T, F> -
Returns an Ok of the original value if
thisis an Ok, or the result ofcalculateOtherotherwise. -
toString(
) → String -
A string representation of this object.
inherited
-
unwrap(
{String? msg}) → T - Returns the contained value.
-
unwrapErr(
{String? msg}) → E - Returns the contained error.
-
unwrapErrOr(
E defaultError) → E -
Returns the contained error, if any, or
defaultErrorotherwise. -
unwrapErrOrElse(
E calculateDefaultError(T value)) → E -
Returns the contained error, if any, or the result of
calculateDefaultErrorotherwise. -
unwrapOr(
T defaultValue) → T -
Returns the contained value, if any, or
defaultValueotherwise. -
unwrapOrElse(
T calculateDefaultValue(E error)) → T -
Returns the contained value, if any, or the result of
calculateDefaultValueotherwise.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
collect<
T, E> (Result< T, E> collector(U check<U>(Result< )) → Result<U, E> result)T, E> - Encloses any number of operations, optionally returning early on Err.
-
collectAsync<
T, E> (FutureOr< Result< collector(U check<T, E> >U>(Result< )) → Future<U, E> result)Result< T, E> > - Encloses any number of operations, optionally returning early on Err.