Maybe<T> class
Dealing with optional values in ui has always been verbose and unsafe. So the
Maybe<T>
generic union type is a convenience type to model and help safelly deal with any optional value outcomes.
Where we can have two types that will represent the state of a value that can be null. The Nothing, representing when it has no value, and the Just, when it has a value.
The approach is declarative, so in order to deal with the states of Maybe, one
should use one of the unions methods.
The .map
forces you to deal with all the two states explicitly, passing callbacks for
each state with undestructured states.
Example:
Maybe<String> someMaybeValue = Just("test");
final debugValue = someMaybeValue.map(
nothing: (_) => "",
just: (data) => data.value,
);
print(debugValue); // test
The .when
forces you to deal with all the two states explicitly, passing callbacks for
each state with destructured states.
Example:
Maybe<String> someMaybeValue = Nothing();
final debugValue = someMaybeValue.map(
nothing: () => "test",
just: (data) => data,
);
print(debugValue); // test
and one also might want to not deal explicitly with all states diferently, so
there are the .maybeMap
, and .maybeWhen
methods where you need only expclitly to pass a
orElse
callback. But I would say that it is not so useful in this case since we only have two states to be treated.
Example:
Maybe<String> someMaybeValue = Just("test");
final debugValue = someMaybeValue.maybeWhen(
just: (data) => data,
orElse() => "",
);
print(debugValue); // test
So, Maybe
provides a safe and declarative way to always deal with the two possible states of a optional value.
Constructors
- Maybe.from(T? input)
-
Factory for helping building a Maybe from a nullable input. It produces a Nothing if the input is null, and a Just otherwise
factory
-
Maybe.fromRequest(RequestStatus<
T> input) -
Factory for helping building a Maybe from a RequestStatus input. It produces a Just if the input is Succeeded, and a Nothing otherwise
factory
-
Maybe.fromResult(Result<
T> input) -
Factory for helping building a Maybe from a Result input. It produces a Nothing if the input is Failure, and a Just if the input is Success
factory
- Maybe.just(T value)
-
Type representing the Just state of a value, which would be equivalent to having a value
constfactory
- Maybe.nothing()
-
Type representing the Nothing state of a value, which would be equivalent to having a null value
constfactory
Properties
-
asJust
→ Just<
T> -
Cast
this
into a Just, and throw an exception if the cast fails!It might be tempting to just cast the Maybe into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - asNothing → Nothing
-
Cast
this
into a Nothing, and throw an exception if the cast fails!It might be tempting to just cast the Maybe into the desired type, but it's
strongly advised to not do that indiscriminately
. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- isJust → bool
-
no setter
- isNothing → bool
-
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getOrElse<
Type> (Type fallback) → Type - The getOrElse method which receives a parameter to return as a fallback value, when the value is a Nothing, or there is no value in the Just.
-
map<
TResult extends Object?> ({required TResult nothing(Nothing< T> value), required TResult just(Just<T> value)}) → TResult -
inherited
-
mapAsyncJust<
K> (FutureOr< Maybe< combiner(T)) → FutureOr<K> >Maybe< K> > -
A Method to chain async access to data held by the Maybe. If
this
is Nothing returns Nothing, ifthis
is Just, returns the result of thecombiner
method over thevalue
inside Just -
mapJust<
K> (Maybe< K> combiner(T)) → Maybe<K> -
A method to chain access to data held by the Maybe. If
this
is Nothing returns Nothing, ifthis
is Just, returns the result of thecombiner
method over thevalue
inside Just -
mapOrNull<
TResult extends Object?> ({TResult? nothing(Nothing< T> value)?, TResult? just(Just<T> value)?}) → TResult? -
inherited
-
maybeMap<
TResult extends Object?> ({TResult nothing(Nothing< T> value)?, TResult just(Just<T> value)?, required TResult orElse()}) → TResult -
inherited
-
maybeWhen<
TResult extends Object?> ({TResult nothing()?, TResult just(T value)?, required TResult orElse()}) → TResult -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
when<
TResult extends Object?> ({required TResult nothing(), required TResult just(T value)}) → TResult -
inherited
-
whenOrNull<
TResult extends Object?> ({TResult? nothing()?, TResult? just(T value)?}) → TResult? -
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited