handle<T> method

T handle<T>({
  1. required T onSuccess(
    1. ResultType data
    ),
  2. required T onFailure(
    1. AppError error
    ),
})

the handle method which has two required parameters an onSuccess callback

Type onSuccess(Type data)

and an onFailure callback

Type onFailure(AppError data)

Where AppError is a convenience type to model errors in the application

Example:

Result<String> asyncTaskResturningStringResult = await someFutureOfResultString();

asyncTaskResturningStringResult.handle(
  onSuccess: (String data) {
    "here one have access to the succesful value of the async task and might use it as desired"
  },
  onFailure: (AppError error) {
    "here one have access to the failure modeled as AppError representing this async task"
  }
);

Implementation

T handle<T>({
  required T Function(ResultType data) onSuccess,
  required T Function(AppError error) onFailure,
}) {
  if (this is Success) {
    return onSuccess((this as Success).data);
  } else {
    return onFailure((this as Failure).error);
  }
}