Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

mapEither & mapEitherAsync

Run each event in its own raise scope: r.raise (and r.ensure / r.bind) becomes a Left, a normal return a Right.

FxEvents<Either<E, R>> FxEvents<T>.mapEither<E, R>(R Function(Raise<E> r, T value) f) FxEvents<Either<E, R>> FxEvents<T>.mapEitherAsync<E, R>(FutureOr<R> Function(Raise<E> r, T value) f)

Lecture

attempt is the boundary conversion — it turns whatever is already on the error channel into a Left. mapEither is the operator you reach for after that, or on a clean source: each event runs inside an fxEither builder, so you write straight-line Dart with r.ensure / r.raise and the result of the whole map is Either<E, R>. A failing event does not cancel the source; later events still arrive.

A thrown exception stays on the error channel — that is the fxEither builder's contract, and it keeps attempt the place where a throw on the stream turns into a value. When a callback both raises and throws, prefer fxEitherCatching inside mapEither so one Either comes out.

mapEitherAsync is the async twin: one event at a time, like asyncMap. fxEitherAsync's rule carries over: a raise must happen inside the awaited chain. A raise from an unawaited future outlives the scope and surfaces as an unhandled zone error instead of a Left.

A source error passes through both operators untouched. Convert those with attempt upstream when you want them as Lefts too.

Once the chain carries Either values, mapRight transforms each success and lets a Left through unchanged (mapLeft is the failure-side twin). It is not called map because FxEvents.map already maps the Either as a whole. Chaining attempt after mapEither does catch a thrown exception, but wraps the result in Either<E, Either<E, R>>; flattenEither collapses that nest — an outer Left stays a Left, an inner Either comes out as it is.

Demo 1 · A raise becomes Left, a return becomes Right

Demo 2 · mapEitherAsync, one event at a time

Try it yourself

Exercise: a thrown exception stays on the error channel, and the chain continues past it.

Related: attempt — the boundary that turns a throw into a Left · fxEither builder — the same raise scope, on a single value · rights / separated — split the resulting Eithers