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.

Parameters:

  • path: The API endpoint path (relative to base URL)
  • data: The request body (can be Map, FormData, or any serializable object)
  • queryParameters: Optional query string parameters
  • onSuccess: Optional callback to transform successful response data
  • onError: Optional callback to transform error response data
  • options: Per-request options (headers, auth requirements, etc.)
  • cacheEnabled: Whether to cache this specific POST response (default: false)
  • cacheLifetimeDuration: How long to cache the response

Note: POST responses are not cached by default since they typically modify data. Set cacheEnabled to true for read-only POST endpoints (e.g., search).

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,
      data: data,
      queryParameters: queryParameters,
      options: options?.toOptions(),
    );

    if (cacheEnabled) {
      await cacheHandler.set(response, cacheLifetimeDuration);
    }

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

    return NetworkResult(
      response: e.response,
      exception: e,
      statusCode: e.response?.statusCode,
      statusMessage: e.response?.statusMessage,
    );
  } catch (e) {
    debugPrint(e.toString());
    return Result.error(exception: Exception(e.toString()));
  }
}