【问题标题】:When I go to the page after removing the element, I get the error Invalid value删除元素后转到页面时,出现错误值无效
【发布时间】:2023-02-25 02:26:00
【问题描述】:

我有一个包含收藏夹用户的主列表。我还可以选择从主列表中删除用户,但要从不同的页面中删除。当我转到另一个页面并删除用户并返回主列表时,删除的用户消失了,但出现错误 Invalid value: Not in inclusive range 0..3: 4

如何修复此错误,以便我可以在另一个页面上删除用户,但当我返回时,不会出现错误?

这是显示收藏用户的主页

if (state is FavoritesUserLoaded) {
         stations = state.users;

         return ListView.builder(
          physics: const BouncingScrollPhysics(),
          itemCount: stations.length,
          itemBuilder: (context, index) {
            for (var i = 0; i < stations.length; i++) {
              stations[i].isFavorite ? selected.add(true) : selected.add(false);
            }

            final String city = stations[index].city ?? '';
            final String address = stations[index].address ?? '';
}

另一个页面,这里我从收藏夹中删除用户

        if (state is FavoritesUserLoaded) {
          if (state.users.isNotEmpty) {
            state.users.removeWhere(
                (element) => element.id == widget.userModel.id);
          }
        }

当我删除用户并返回主列表时,我得到了这个错误

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    该问题与您正在从另一个页面without notifying列表已更改的主页修改收藏用户列表有关。当你回到主列表时,它仍然认为列表的长度和以前一样,但现在它实际上更小了,导致out-of-bounds错误。

    您可以添加新的eventmethod进行更新,删除记录后添加emit新的state

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 1970-01-01
      • 2019-05-12
      • 2022-01-14
      • 2021-09-13
      • 2019-01-22
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      相关资源
      最近更新 更多