networkRequestHandler<T> function

Future<void> networkRequestHandler<T>({
  1. required Future<BaseResponse<T>> apiCall(),
  2. dynamic onSuccess(
    1. T?
    )?,
  3. dynamic onError(
    1. String
    )?,
})

Generic API request handler

Implementation

Future<void> networkRequestHandler<T>({
  required Future<BaseResponse<T>> Function() apiCall,
  Function(T?)? onSuccess,
  Function(String)? onError,
}) async {
  try {
    final response = await apiCall();
    if (response.success == Constant.successResCheckValue) {
      onSuccess?.call(response.data);
    } else {
      onError?.call(response.message ?? "Unknown error occurred.");
    }
  } on DioException catch (dioError) {
    // Handling network or API request errors
    onError?.call(_getDioErrorMessage(dioError));
  } catch (e) {
    // Generic error fallback
    onError?.call("Unexpected error: ${e.toString()}");
  }
}