request method
Future
request({
- required BuildContext context,
- required String api,
- required Method method,
- Map<
String, dynamic> ? params, - dynamic payloadObj,
Implementation
Future<dynamic> request(
{required BuildContext context,
required String api,
required Method method,
Map<String, dynamic>? params,
dynamic payloadObj}) async {
Dio _dio = Dio();
_dio = Dio(BaseOptions(
baseUrl: await Constants.baseUrl(),
//connectTimeout: 5000,
//receiveTimeout: 3000,
))
..interceptors.add(Logging());
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient dioClient) {
dioClient.badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
return dioClient;
};
var response;
var payload = json.encode(payloadObj);
try {
if (method == Method.POST) {
response = await _dio
.post(api, queryParameters: params)
.timeout(Constants.timeout);
} else if (method == Method.FORM) {
response =
await _dio.post(api, data: payload).timeout(Constants.timeout);
} else if (method == Method.DELETE) {
response = await _dio.delete(api).timeout(Constants.timeout);
} else if (method == Method.PATCH) {
response = await _dio.patch(api).timeout(Constants.timeout);
} else {
response = await _dio
.get(api, queryParameters: params)
.timeout(Constants.timeout);
}
return response.data;
} on DioError catch (e) {
return Utils.showSnackbar(context, Strings.error,
Utils.handleErrorComing(e).toTitleCase(), Colors.red);
}
}