Maybe<T> class
sealed
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 dart patern matching. /// Example:
Maybe<String> someMaybeValue = Just("test");
final debugValue = switch(someMaybeValue) {
Nothing() => "",
Just(:final value) => value,
};
print(debugValue); // test
So, Maybe
provides a safe and declarative way to always deal with the two possible states of a optional value.
- Annotations
-
- @freezed
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 aJust
otherwisefactory -
Maybe.fromRequest(RequestStatus<
T> input) -
Factory for helping building a Maybe from a RequestStatus input. It produces a
Just
if the input isSucceeded
, and aNothing
otherwisefactory -
Maybe.fromResult(Result<
T> input) -
Factory for helping building a Maybe from a Result input. It produces a
Nothing
if the input isFailure
, and aJust
if the input isSuccess
factory - Maybe.just(T value)
-
Type representing the
Just
state of a value, which would be equivalent to having a valueconstfactory - Maybe.nothing()
-
Type representing the
Nothing
state of a value, which would be equivalent to having a null valueconstfactory
Properties
-
asJust
→ Just<
T> -
Cast
this
into aJust
, 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 aNothing
, 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 theJust
. -
mapAsyncJust<
K> (FutureOr< Maybe< combiner(T)) → FutureOr<K> >Maybe< K> > -
A Method to chain async access to data held by the Maybe. If
this
isNothing
returnsNothing
, ifthis
isJust
, returns the result of thecombiner
method over thevalue
insideJust
-
mapJust<
K> (Maybe< K> combiner(T)) → Maybe<K> -
A method to chain access to data held by the Maybe. If
this
isNothing
returnsNothing
, ifthis
isJust
, returns the result of thecombiner
method over thevalue
insideJust
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited