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
Nothingif the input is null, and aJustotherwisefactory -
Maybe.fromRequest(RequestStatus<
T> input) -
Factory for helping building a Maybe from a RequestStatus input. It produces a
Justif the input isSucceeded, and aNothingotherwisefactory -
Maybe.fromResult(Result<
T> input) -
Factory for helping building a Maybe from a Result input. It produces a
Nothingif the input isFailure, and aJustif the input isSuccessfactory - Maybe.just(T value)
-
Type representing the
Juststate of a value, which would be equivalent to having a valueconstfactory - Maybe.nothing()
-
Type representing the
Nothingstate of a value, which would be equivalent to having a null valueconstfactory
Properties
-
asJust
→ Just<
T> -
Cast
thisinto aJust, and throw an exception if the cast fails!It might be tempting to just cast the Maybe into the desired type, but it'sstrongly advised to not do that indiscriminately. Although, it might be convenient to have this cast sometimes. Use it wisely!no setter - asNothing → Nothing
-
Cast
thisinto aNothing, and throw an exception if the cast fails!It might be tempting to just cast the Maybe into the desired type, but it'sstrongly 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
thisisNothingreturnsNothing, ifthisisJust, returns the result of thecombinermethod over thevalueinsideJust -
mapJust<
K> (Maybe< K> combiner(T)) → Maybe<K> -
A method to chain access to data held by the Maybe. If
thisisNothingreturnsNothing, ifthisisJust, returns the result of thecombinermethod over thevalueinsideJust -
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