post static method
Sends a POST
request to the given methodName
with the provided params
.
- Checks internet connectivity before making the request.
- Logs request details for debugging.
- Returns a ResponseAPI object containing the response data.
If an error occurs, it is handled and returned as an error response.
Implementation
static Future<ResponseAPI> post({
required String methodName,
required Map<String, dynamic> params,
}) async {
try {
ResponseAPI? interNetMap = await _checkConnectivity();
if (interNetMap != null) {
return interNetMap; // Return error response if no internet connection.
}
String url = ApiConstant.baseUrl + methodName;
Options options = Options(
headers: {"Content-Type": "application/json"},
);
logMessage("==request== $url");
logMessage("==params== $params");
Response response = await _dio.post(url, data: params, options: options);
logMessage("==response== ${response.data}");
return ResponseAPI(response.statusCode ?? 0, response.data);
} catch (error) {
logMessage("==error==$error");
return _handleError(error); // Handle and return error response.
}
}