postData<T> function
Generic POST request
Implementation
Future<T> postData<T>({
required String endpoint,
required Map<String, dynamic> body,
T Function(dynamic json)? fromJson,
}) async {
if (_baseUrl.isEmpty) {
throw Exception("Base URL is not set. Call setBaseUrl() first.");
}
final response = await http.post(
Uri.parse("$_baseUrl/$endpoint"),
headers: _buildHeaders(),
body: jsonEncode(body),
);
if (response.statusCode == 200 || response.statusCode == 201) {
final json = jsonDecode(response.body);
return fromJson != null ? fromJson(json) : json;
} else {
throw Exception("POST $endpoint failed: ${response.statusCode}");
}
}