【问题标题】:Flutter Invalid value: not in inclusive rangeFlutter 无效值:不在包含范围内
【发布时间】:2021-02-17 11:48:59
【问题描述】:

我一直在尝试在列表视图构建器的末尾添加一个按钮。我试图做这个问题中建议的事情:Flutter: How to add a button widget at the end of a ListView.builder that contains other type of widgets?。但如果我这样做,我会得到:“RangeError (index): Invalid value: Not inclusive range 0..49: 50

我尝试查看也有此问题的问题,但我找不到修复它的答案。

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,
      );

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    删除下面的行并在 if 条件之后添加它。

    删除这一行:

     Anime anime = animeList[index];
    

    if 条件后删除的行

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

    【讨论】:

    • 你能解释一下为什么会这样吗?因为我不明白
    【解决方案2】:

    你正在使用

    itemCount: animeList.length + 1,
    

    你应该使用

    itemCount: animeList.length,
    

    在 ListView 中,索引从零开始,而不是一

    【讨论】:

    • 这会使错误消失,但按钮仍然没有显示在列表的末尾
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 2021-02-04
    • 1970-01-01
    • 2021-01-01
    • 2022-11-23
    • 2022-01-25
    • 2021-08-14
    • 2021-10-21
    相关资源
    最近更新 更多