fold<R> method

FutureOr<R> fold<R>({
  1. required FutureOr<R> onSuccess(
    1. S value
    ),
  2. required FutureOr<R> onFailure(
    1. int statusCode,
    2. String? message,
    3. String? error
    ),
})

Method to handle both success and failure cases

Implementation

FutureOr<R> fold<R>(
    {required FutureOr<R> Function(S value) onSuccess,
    required FutureOr<R> Function(
            int statusCode, String? message, String? error)
        onFailure}) async {
  // If the result is a success, call the onSuccess function with the value
  if (this is MapboxSearchSuccess) {
    return await onSuccess((this as MapboxSearchSuccess<S>).value);
  }
  // If the result is a failure, call the onFailure function with the status code, message, and error
  else if (this is MapboxSearchFailure) {
    return await onFailure(
        (this as MapboxSearchFailure<S>).statusCode,
        (this as MapboxSearchFailure<S>).message,
        (this as MapboxSearchFailure<S>).error);
  }
  // Throw an exception if the result type is unexpected
  else {
    throw Exception('Unexpected result type');
  }
}