post method
Implementation
Future<Map<String, dynamic>> post(String url, Map<String, dynamic> body) async {
try {
final response = await http.post(
Uri.parse(url),
headers: {"Content-Type": "application/json"},
body: jsonEncode(body),
);
if (response.statusCode == 200) {
// convert response body to Map
final data = jsonDecode(response.body);
if (data is Map<String, dynamic>) {
return data;
} else {
throw Exception("Response is not a valid JSON object");
}
} else {
throw Exception("Failed with status code: ${response.statusCode}");
}
} catch (e) {
throw Exception("API error: $e");
}
}