whenResult<T> method
TransitionHandlerWhenResultBuilder<D>
whenResult<T>(
- FutureOr<
Result< result(T> >- TransitionHandlerContext<
D, C> ctx
- TransitionHandlerContext<
- void buildSuccess(
- TransitionHandlerBuilder<
D, T> builder
- TransitionHandlerBuilder<
- String? label,
Describes transition behavior that runs conditionally, depending on a Result value.
When the transition occurs, the result function is evaluated, and the returned Result is
used to determine the transition behavior. If Result.isValue is true, then the behavior
described by the buildSuccess callback will take place. If Result.isError is true, then
an exception will be raised. However, TransitionHandlerWhenResultBuilder.otherwise can be
used to override the default error handling.
Implementation
TransitionHandlerWhenResultBuilder<D> whenResult<T>(
FutureOr<Result<T>> Function(TransitionHandlerContext<D, C> ctx) result,
void Function(TransitionHandlerBuilder<D, T> builder) buildSuccess, {
String? label,
}) {
var resultRef = Ref<Result<T>?>(null);
var successBuilder = TransitionHandlerBuilder<D, T>._(
_forState,
_log,
// This will only be called when the result is succesful, so this crazy property path will be
// valid.
(_) => resultRef.value!.asValue!.value);
var failureBuilder = TransitionHandlerBuilder<D, AsyncError>._(_forState, _log, (_) {
var err = resultRef.value!.asError!;
return AsyncError(err.error, err.stackTrace);
});
buildSuccess(successBuilder);
var successDesr = successBuilder._descriptor;
if (successDesr == null) {
throw StateError(
'No success handler was defined for whenResult. Make sure to define the success '
'handler in the buildSuccessHandler function.');
}
var failureDescrRef = Ref<TransitionHandlerDescriptor<AsyncError>?>(null);
_descriptor = makeWhenResultTransitionDescriptor<C, D, T>(
_forState,
result,
_makeContext,
resultRef,
successDesr,
failureDescrRef,
_log,
label,
);
return TransitionHandlerWhenResultBuilder<D>(() => failureBuilder, failureDescrRef);
}