request method

Future request({
  1. required String url,
  2. required Method method,
  3. Map<String, dynamic>? params,
  4. Options? options,
  5. FormData? formdata,
})

Implementation

Future<dynamic> request(
    {required String url,
    required Method method,
    Map<String, dynamic>? params,
    Options? options,
    FormData? formdata}) async {
  Response response;

  try {
    if (method == Method.POST) {
      if (formdata == null) {
        response = await dio!.post(url, data: params, options: Options(
          validateStatus: (status) {
            return true;
          },
        ));
      } else {
        response = await dio!.post(url, data: formdata, options: options);
      }
    } else if (method == Method.DELETE) {
      response = await dio!.delete(url);
    } else if (method == Method.PATCH) {
      response = await dio!.patch(url);
    } else {
      response = await dio!.get(url);
    }
    return response;
  } on SocketException catch (e) {
    throw Exception("Not Internet Connection");
  } on FormatException catch (e) {
    throw Exception("Bad response format");
  } on DioError catch (e) {
    return e.response?.data.toString();
  } catch (e) {
    logger.e(e);
    throw Exception("Something went wrong");
  }
}