putData<T> function

Future<T> putData<T>({
  1. required String endpoint,
  2. required Map<String, dynamic> body,
  3. T fromJson(
    1. dynamic json
    )?,
})

Generic PUT request

Implementation

Future<T> putData<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.put(
    Uri.parse("$_baseUrl/$endpoint"),
    headers: _buildHeaders(),
    body: jsonEncode(body),
  );

  if (response.statusCode == 200) {
    final json = jsonDecode(response.body);
    return fromJson != null ? fromJson(json) : json;
  } else {
    throw Exception("PUT $endpoint failed: ${response.statusCode}");
  }
}