getEntries<T> method
Fetch and parse the given type of Contentful model. Parses the language to utf8. Throws Exception on error.
Implementation
Future<ContentfulDeliveryDataModel<T>> getEntries<T>({
required T Function(Object?) fromJsonT,
String? contentType,
Map<String, dynamic>? query,
}) async {
final contentTypeParam =
contentType != null ? '&content_type=$contentType' : '';
final baseUrl = '$_baseUrl/spaces/${_client.spaceId}/environments'
'/${_client.environmentId}/entries?access_token=${_client.accessToken}'
'&locale=${_client.locale.languageCode}$contentTypeParam';
// Append query parameters to the URL if provided
final url =
query != null ? '$baseUrl&${_buildQueryString(query)}' : baseUrl;
final response = await http.get(Uri.parse(url));
Logger().i('Fetching entries response: ${response.statusCode}');
if (response.statusCode == 200) {
final body = utf8.decode(response.bodyBytes);
final jsonBody = jsonDecode(body) as Map<String, dynamic>?;
if (jsonBody == null) return throw Exception('Failed to load data');
final result = ContentfulDeliveryDataModel.fromJson(jsonBody, fromJsonT);
return result;
} else {
throw Exception('Failed to load article');
}
}