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 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
const
factory
Maybe.nothing()
Type representing the Nothing state of a value, which would be equivalent to having a null value
const
factory

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.
mapAsyncJust<K>(FutureOr<Maybe<K>> combiner(T)) FutureOr<Maybe<K>>
A Method to chain async access to data held by the Maybe. If this is Nothing returns Nothing, if this is Just, returns the result of the combiner method over the value 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, if this is Just, returns the result of the combiner method over the value inside Just
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