0
votes

I have been trying to add an button to the end of a listview builder. I tried to do what was suggested in this question: Flutter: How to add a button widget at the end of a ListView.builder that contains other type of widgets?. But if i do that i get: "RangeError (index): Invalid value: Not in inclusive range 0..49: 50

I tried looking at questions that also had this problem but i couldnt find an anwser that fixed it.

List<Anime> animeList = animeData.animeList;
      print(animeList.length);
      return ListView.builder(
        padding: EdgeInsets.only(bottom: 30, top: 10),
        itemBuilder: (context, index) {
          Anime anime = animeList[index];

          if (index == animeList.length) {
            return ThinOutlineBtn(
              primaryColor: themeWizard.getPrimaryColor(),
              darkColor: themeWizard.getBackgroundColor(),
              text: "More",
              highlightedBorderColor: themeWizard.getPrimaryColor(),
              ontap: () {
                AnilistApi.followUpQuery(AnilistApi.animeQuery, animeData,
                    animeData.animePage.currentPage + 1);
              },
            );
          }

          return AnimeCard(
            anime: anime,
            darkMode: themeWizard.getCurrentMode(),
            activeIcon: themeWizard.getPrimaryColor(),
            background: themeWizard.getCardColor(),
            btnHighlightColor: themeWizard.getBackgroundColor(),
            textColor: themeWizard.getCardTextColor(),
            inActiveIcon: themeWizard.getIconColor(),
          );
        },
        itemCount: animeList.length + 1,
      );
2

2 Answers

1
votes

Remove the below line and add it after if condition.

Remove this line:

 Anime anime = animeList[index];

And the removed line after if condition

if (index == animeList.length) {
....
.....
}
/// Add removed line here
Anime anime = animeList[index];
0
votes

You are using

itemCount: animeList.length + 1,

You should use

itemCount: animeList.length,

In ListView, indexes starts with ZEROES, not ONE