loadChapter method

dynamic loadChapter({
  1. int index = -1,
})

Implementation

loadChapter({int index = -1}) async {
  chaptersList = [];

  await Future.wait(epubBook.Chapters!.map((EpubChapter chapter) async {
    String? chapterTitle = chapter.Title;
    List<LocalChapterModel> subChapters = [];
    for (var element in chapter.SubChapters!) {
      subChapters.add(
          LocalChapterModel(chapter: element.Title!, isSubChapter: true));
    }

    chaptersList.add(LocalChapterModel(
        chapter: chapterTitle ?? '...', isSubChapter: false));

    chaptersList += subChapters;
  }));

  ///Choose initial chapter
  if (widget.starterChapter >= 0 &&
      widget.starterChapter < chaptersList.length) {
    setupNavButtons();
    await updateContentAccordingChapter(
        index == -1 ? widget.starterChapter : index);
  } else {
    setupNavButtons();
    await updateContentAccordingChapter(0);
    CustomToast.showToast(
        "Invalid chapter number. Range [0-${chaptersList.length}]");
  }
}