flutter - Flutter:RangeError(索引):无效值:不在0..14范围内,包括15

标签 flutter dart flutter-layout bloc

我正在使用bloc设计模式构建一个ListView,并且(flutter_bloc lib)将该列表分页并且只需放置列表长度并返回小部件即可正常工作,

但是,当我尝试在列表下添加指示器并为itemCount添加+ 1以加载UI时,就会发生问题

错误

Restarted application in 1,900ms.

[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following RangeError was thrown building:[39;49m
RangeError (index): Invalid value: Not in range 0..14, inclusive: 15

[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      List.[]  (dart:core-patch/growable_array.dart:149:60)[39;49m
[38;5;248m#1      _HomePageState.build.<anonymous closure>.<anonymous closure>[39;49m
[38;5;244m#2      SliverChildBuilderDelegate.build[39;49m
[38;5;244m#3      SliverMultiBoxAdaptorElement._build.<anonymous closure>[39;49m
[38;5;244m#4      _HashMap.putIfAbsent  (dart:collection-patch/collection_patch.dart:139:29)[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m


列表的代码

return BlocListener<PostsBloc, PostsState>(
  listener: (context, state) {
    if ( state is PostsError ) {
      Scaffold.of(context).showSnackBar(
        SnackBar(content: Text(state.errMsg))
      );
    }
  },        
  child: BlocBuilder<PostsBloc, PostsState>(builder: (context, state) {
    if (state is PostsInitial || state is PostsLoading) {
      return loading();
    } else if (state is PostsLoaded) {
      return ListView.builder(
        itemCount: state.hasReachedMax
            ? state.posts.length
            : state.posts.length + 1,
        controller: _scrollController,
        itemBuilder: (BuildContext context, int i) {
          final Post post = state.posts[i];

          return (i >= state.posts.length) 
          ? Center(child: CircularProgressIndicator())
          : ListTile(leading: Text(i.toString()), title: Text(post.title, style: TextStyle(fontWeight: FontWeight.bold),));
        }
      );
    }

    return errorMsg();
  }),
);

最佳答案

首先检查长度。如果数组不会超出范围,则仅尝试使用最终访问元素。post post = state.posts [i];

还没有尝试过,但我希望这对您有用。

itemBuilder: (BuildContext context, int i) {
          if(i >= state.posts.length){
            return Center(child: CircularProgressIndicator());
          }
          final Post post = state.posts[i];
          ListTile(leading: Text(i.toString()), title: Text(post.title, style: TextStyle(fontWeight: FontWeight.bold),));
        }
```

关于flutter - Flutter:RangeError(索引):无效值:不在0..14范围内,包括15,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60761439/

相关文章:

android - Flutter: InternalLinkedHashMap<String, dynamic >' has no instance method ' cast' with matching arguments

dependencies - polymer Dart 的依赖

function - Dart:从函数访问调用者实例

flutter - 如何在 flutter 中对齐行和列中的点?

flutter - 如何在 flutter 中制作响应式容器

flutter 。将一个 Draggable 项拖到另一个 Draggable 项上

firebase - Flutter:Stream已经被监听了

Flutter ScrollController 抛出异常

Dart StageXL 在重叠的 Sprite 上激活鼠标事件

flutter - Flutter:如何根据对象的字段值在 'set'中搜索对象