search method Null safety
Search for manga
Optional arguments are mangaName
(String), authors
, includedTags
, excludedTags
, status
& demographic
( All List<String>)
status
MUST BE ONE OF ["ongoing","completed","hiatus","cancelled"]
For more info see the official documentation
Throws Exception if the server returns a 404 error
Returns an empty List<String> if no results
Implementation
Future<List<Manga>> search(
{String mangaTitle = '',
List<String> authors = const [],
List<String> includedTags = const [],
List<String> excludedTags = const [],
List<String> status = const [],
List<String> demographic = const []}) async {
var res;
if (token != '') {
res = await http.get(
Uri.parse(
'https://api.mangadex.org/manga?title=$mangaTitle${(authors.isNotEmpty) ? '&authors[]=${authors.join('&authors[]=')}' : ''}${(includedTags.isNotEmpty) ? '&includedTags[]=${includedTags.join('&includedTags[]=')}' : ''}${(excludedTags.isNotEmpty) ? '&excludedTags[]=${excludedTags.join('&excludedTags[]=')}' : ''}${(status.isNotEmpty) ? '&status[]=${status.join('&status[]=')}' : ''}${(demographic.isNotEmpty) ? '&publicationDemographic[]=${demographic.join('&publicationDemographic[]=')}' : ''}'),
headers: {HttpHeaders.authorizationHeader: 'Bearer $token'});
} else {
res = await http.get(Uri.parse(
'https://api.mangadex.org/manga?title=$mangaTitle${(authors.isNotEmpty) ? '&authors[]=${authors.join('&authors[]=')}' : ''}${(includedTags.isNotEmpty) ? '&includedTags[]=${includedTags.join('&includedTags[]=')}' : ''}${(excludedTags.isNotEmpty) ? '&excludedTags[]=${excludedTags.join('&excludedTags[]=')}' : ''}${(status.isNotEmpty) ? '&status[]=${status.join('&status[]=')}' : ''}${(demographic.isNotEmpty) ? '&publicationDemographic[]=${demographic.join('&publicationDemographic[]=')}' : ''}'));
}
var data = jsonDecode(res.body);
if (res.statusCode == 400) {
throw 'Error: ${data["errors"][0]["title"]} - ${data["errors"][0]["detail"]}';
}
List<Manga>? results = [];
for (var manga in data['results']) {
var r = manga['data'];
results.add(Manga(
altTitles: r['attributes']['altTitles'],
title: Map.from(r['attributes']['title']),
tags: r['attributes']['tags'],
description: Map.from(r['attributes']['description']),
isLocked: r['attributes']['isLocked'],
links: (r['attributes']['links'] == null)
? null
: Map.from(r['attributes']['links']),
originalLang: r['attributes']['originalLanguage'],
lastChapter: r['attributes']['lastChapter'],
lastVolume: r['attributes']['lastVolume'],
demographic: r['attributes']['publicationDemographic'],
status: r['attributes']['status'],
releaseYear: r['attributes']['year'],
contentRating: r['attributes']['contentRating'],
createdAt: r['attributes']['createdAt'],
updatedAt: r['attributes']['updatedAt'],
id: r['id']));
}
return results;
}