flatMap<B> abstract method

  1. @override
Option<B> flatMap<B>(
  1. covariant Option<B> f(
    1. T t
    )
)
override

Used to chain multiple functions that return a Option.

You can extract the value of every Option in the chain without handling all possible missing cases. If any of the functions in the chain returns None, the result is None.

/// Using `flatMap`, you can forget that the value may be missing and just
/// use it as if it was there.
///
/// In case one of the values is actually missing, you will get a [None]
/// at the end of the chain ⛓
final a = Option.of('name');
final Option<double> result = a.flatMap(
  (s) => stringToInt(s).flatMap(
    (i) => intToDouble(i),
  ),
);

👇

[😀].flatMap(
    (😀) => [👻(😀)]
    ) -> [😱]

[😀].flatMap(
    (😀) => [👻(😀)]
    ).flatMap(
        (😱) => [👨‍⚕️(😱)]
        ) -> [🤕]

[😀].flatMap(
    (😀) => [_]
    ).flatMap(
        (_) => [👨‍⚕️(_)]
        ) -> [_]

[_].flatMap(
    (😀) => [👻(😀)]
    ) -> [_]

Implementation

@override
Option<B> flatMap<B>(covariant Option<B> Function(T t) f);