getManga method Null safety

Future<Manga?> getManga(
  1. String uuid,
  2. {bool appendChapters = false,
  3. List<String> translatedLang = const [],
  4. bool useLogin = false}
)

Gets information about manga

If appendChapters is true, returns available chapters in .chapters If there are no chapters available or appendChapters is false, .chapters returns an empty Map If appending chapters, translatedLang should be either an empty Array (which will append chapters of all languages) or filled with codes of desired languages

Returns Null if no manga can be found

Implementation

Future<Manga?> getManga(String uuid,
    {bool appendChapters = false,
    List<String> translatedLang = const [],
    bool useLogin = false}) async {
  var res;
  if (token != '' && useLogin) {
    res = await http.get(
        Uri.parse(
            'https://api.mangadex.org/manga/$uuid?includes[]=cover_art&includes[]=author&includes[]=artist'),
        headers: {
          HttpHeaders.authorizationHeader: 'Bearer $token',
          HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'
        });
  } else {
    res = await http.get(
        Uri.parse(
            'https://api.mangadex.org/manga/$uuid?includes[]=cover_art&includes[]=author&includes[]=artist'),
        headers: {HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'});
  }
  var body = jsonDecode(res.body);
  var data = body['data'];
  var relations = body['relationships'];
  if (res.statusCode == 403 && res.headers['X-Captcha-Sitekey'] != null) {
    throw CaptchaException(res.headers['X-Captcha-Sitekey'],
        message:
            'You need to solve a captcha, check `.sitekey` for the sitekey.');
  }
  // ignore: omit_local_variable_types
  Map<String, dynamic> chapters = {};
  // append available chapters from other API endpoint
  if (appendChapters) {
    var chres = await http.get(
        Uri.parse('https://api.mangadex.org/manga/$uuid/aggregate'),
        headers: {HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'});
    chapters = Map.from(jsonDecode(chres.body)['volumes']);
  }
  var cover, author, artist;
  for (var rel in relations) {
    switch (rel['type']) {
      case 'author':
        author = Author(
            name: rel['attributes']['name'],
            id: rel['id'],
            biography: rel['attributes']['biography'],
            imageUrl: rel['attributes']['imageUrl']);
        break;
      case 'artist':
        artist = Author(
            name: rel['attributes']['name'],
            id: rel['id'],
            imageUrl: rel['attributes']['imageUrl'],
            biography: rel['attributes']['biography']);
        break;
      case 'cover_art':
        cover =
            'https://uploads.mangadex.org/covers/$uuid/${rel['attributes']['fileName']}';
        break;
      default:
        break;
    }
  }
  var manga = Manga(
    altTitles: data['attributes']['altTitles'],
    title: Map.from(data['attributes']['title']),
    tags: data['attributes']['tags'],
    description: Map.from(data['attributes']['description']),
    isLocked: data['attributes']['isLocked'],
    links: (data['attributes']['links'] == null)
        ? null
        : Map.from(data['attributes']['links']),
    originalLang: data['attributes']['originalLanguage'],
    lastChapter: data['attributes']['lastChapter'],
    lastVolume: data['attributes']['lastVolume'],
    demographic: data['attributes']['publicationDemographic'],
    status: data['attributes']['status'],
    releaseYear: data['attributes']['year'],
    contentRating: data['attributes']['contentRating'],
    createdAt: data['attributes']['createdAt'],
    updatedAt: data['attributes']['updatedAt'],
    id: data['id'],
    chapters: chapters,
    author: author,
    artist: artist,
    cover: cover,
  );
  return manga;
}