apiPost method

Future apiPost({
  1. required String endpoint,
  2. required Map<String, dynamic> body,
  3. required BuildContext context,
})

POST method for REST API call

Pass UserModel as user required for authentication based on app token

Pass String as endpoint

Implementation

Future<dynamic> apiPost({
  required String endpoint,
  required Map<String, dynamic> body,
  required BuildContext context,
}) async {
  try {
    // String? token = await FirebaseAuth.instance.currentUser!.getIdToken();
    _dio.options.headers["x-api-key"] = _apiKey;
    _dio.options.headers["x-user-id"] = _userId;
    // log("API POST CALL : ${apiBaseUrl + endpoint}");
    dio.Response rawResponse = await _dio.post(apiBaseUrl + endpoint,
        data: body,
        options: dio.Options(
          followRedirects: false,
          validateStatus: (status) {
            return status! < 300;
          },
        ));

    return rawResponse.data;
  } on dio.DioException catch (e) {
    log(e.response!.data.toString());
    throw ApiError(code: 400, message: e.response!.data['message']);

    // log(e.error.toString());
    // log(e.response.toString());
  }
}