post method

Future post(
  1. String endpoint, {
  2. String? jsonData,
})

Implementation

Future<dynamic> post(String endpoint, {String? jsonData}) async {
  Map<String, String> headers = {
    "Content-Type": "application/json",
  };

  if (apiKey != null) {
    headers["Authorization"] = "Bearer $apiKey";
  }

  final response = await http.post(
    Uri.parse('$baseUrl/$endpoint'),
    headers: headers,
    body: jsonData,
  );

  // Logging the response body for better clarity

  if (response.statusCode == 200) {
    return json.decode(response.body);
  } else {
    // Better error handling
    // You could throw a custom exception here or handle the error based on the status code
    throw Exception('Failed to post data: Status code ${response.statusCode}, ${response.reasonPhrase}');
  }
}