requestMediaWithKeyword method
main request method for all kind of media types
Implementation
Future<PixabayResponse?> requestMediaWithKeyword(
{MediaType? media,
String? keyword,
int resultsPerPage = 30,
int? page,
String? category}) async {
// Search for media associated with the keyword
String url = "https://pixabay.com/api/";
PixabayResponse? res;
if (media == MediaType.video) url += "videos/";
if (resultsPerPage < 3) // API restriction
resultsPerPage = 3;
url += "?key=" + apiKey;
if (keyword != null) url += "&q=" + Uri.encodeFull(keyword);
url +=
"&lang=" + Uri.encodeFull(this.language) + "&per_page=$resultsPerPage";
url += "&safesearch=${this.safeSearch}";
if (category != null) url += "&category=$category";
if (page != null) url += "&page=$page";
if (media == MediaType.video) {
final jsonString = await getVideos(url);
final data = jsonDecode(jsonString);
if (data != null && data.length > 0) {
List<PixabayVideo> videos =
List<PixabayVideo>.generate(data['hits'].length, (index) {
return PixabayVideo.fromJson(data['hits'][index]);
});
res = PixabayResponse(
body: jsonString,
total: data["total"],
totalHits: data["totalHits"],
hits: videos);
}
} else {
print('url is: '+url);
final jsonString = await getImages(url);
final data = jsonDecode(jsonString);
res = bodyToPixabayResponse(data, jsonString);
}
return res;
}