callApi static method
Sends an HTTP request to the provided API endpoint.
This method supports common HTTP methods such as GET, POST, PUT, and DELETE. The response is returned as JSON and can optionally be used to generate Dart models automatically.
Implementation
static Future<Map<String, dynamic>> callApi({
required String url,
required String method,
Map<String, dynamic>? body,
}) async {
http.Response response;
final headers = {
"Content-Type": "application/json"
};
switch (method.toUpperCase()) {
case "POST":
response = await http.post(
Uri.parse(url),
headers: headers,
body: jsonEncode(body ?? {}),
);
break;
case "PUT":
response = await http.put(
Uri.parse(url),
headers: headers,
body: jsonEncode(body ?? {}),
);
break;
case "DELETE":
response = await http.delete(
Uri.parse(url), headers: headers);
break;
default:
response = await http.get(
Uri.parse(url), headers: headers);
}
return jsonDecode(response.body);
}