postRequest method

Future postRequest(
  1. String endUrl,
  2. dynamic body, {
  3. String? contentType,
  4. Decoder? decoder,
  5. Map<String, String>? headers,
  6. Map<String, dynamic>? query,
})

API Post Method

Implementation

Future<dynamic> postRequest(
  String endUrl,
  dynamic body, {
  String? contentType,
  Decoder? decoder,
  Map<String, String>? headers,
  Map<String, dynamic>? query,
}) async {
  _debugLog('POST Request - URL: $endUrl');
  _debugLog('POST Body: $body');

  final connectivityResult = await (Connectivity().checkConnectivity());
  if (connectivityResult.length <= 1 &&
      connectivityResult.first == ConnectivityResult.none) {
    _debugLog('No internet connection detected', isError: true);
    throw _handleInternetError();
  }
  try {
    headers ?? _headers;
    _debugLog('Sending POST request to: $_baseUrl$endUrl');
    final response = await post(endUrl, body,
        contentType: contentType,
        decoder: decoder,
        headers: headers,
        query: query);
    _debugLog('POST Response - Status: ${response.statusCode}');
    return processAndHandleResponse(response);
  } on GetHttpException catch (ex) {
    _debugLog('POST GetHttpException: ${ex.message}', isError: true);
    throw _handleError(ex);
  } on UnauthorizedException catch (ex) {
    _debugLog('POST UnauthorizedException: $ex', isError: true);
    throw _handleError(ex);
  } on UnexpectedFormat catch (ex) {
    _debugLog('POST UnexpectedFormat: ${ex.message}', isError: true);
    throw _handleError(ex);
  } on Exception catch (ex) {
    _debugLog('POST Exception: $ex', isError: true);
    throw _handleError(ex);
  } on FlutterErrorDetails catch (ex) {
    _debugLog('POST FlutterErrorDetails: ${ex.exceptionAsString()}',
        isError: true);
    throw _handleError(ex);
  }
}