post<T> method

  1. @override
Future<Result<T>> post<T>(
  1. String path, {
  2. dynamic data,
  3. Map<String, dynamic>? queryParameters,
  4. FutureOr<T> onSuccess(
    1. dynamic data
    )?,
  5. FutureOr<T> onError(
    1. dynamic data
    )?,
  6. RestApiClientRequestOptions? options,
  7. bool cacheEnabled = false,
  8. Duration? cacheLifetimeDuration,
})
override

Performs a POST request to the specified path with optional data and query parameters.

Implementation

@override
Future<Result<T>> post<T>(
  String path, {
  data,
  Map<String, dynamic>? queryParameters,
  FutureOr<T> Function(dynamic data)? onSuccess,
  FutureOr<T> Function(dynamic data)? onError,
  RestApiClientRequestOptions? options,
  bool cacheEnabled = false,
  Duration? cacheLifetimeDuration,
}) async {
  try {
    final response = await _dio.post(
      path, // The endpoint to hit
      data: data, // Data to send in the request body
      queryParameters: queryParameters, // Query parameters
      options: options?.toOptions(), // Additional Dio options
    );

    // Cache response if caching is enabled
    if (cacheEnabled) {
      await cacheHandler.set(response, cacheLifetimeDuration);
    }

    return NetworkResult(
      response: response,
      data: await _resolveResult(
        response.data,
        onSuccess,
      ), // Resolve result data
    );
  } on DioException catch (e) {
    await exceptionHandler.handle(
      e,
      silent: options?.silentException,
    ); // Handle Dio exceptions

    return NetworkResult(
      response: e.response, // Return error response
      exception: e, // Return the exception
      statusCode: e.response?.statusCode, // HTTP status code
      statusMessage: e.response?.statusMessage, // HTTP status message
    );
  } catch (e) {
    debugPrint(e.toString()); // Print any exceptions

    return Result.error(
      exception: Exception(e.toString()),
    ); // Return a generic error
  }
}