callApi static method

Future<Map<String, dynamic>> callApi({
  1. required String url,
  2. required String method,
  3. Map<String, dynamic>? body,
})

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);
}