简体   繁体   English

RangeError(索引):无效值:不在 0..7 范围内,包括:8

[英]RangeError (index): Invalid value: Not in range 0..7, inclusive: 8

I'm trying to display all the values from retrieved from an API request.我正在尝试显示从 API 请求中检索到的所有值。

Here's what it's showing at the moment:这是它目前显示的内容:

文本

I'm using a listview builder for the page.我正在为页面使用列表视图构建器。 The JSON can be retrieved from this link: Here可以从此链接检索 JSON: 此处

This is my entire code for the page: Code这是我的整个页面代码代码

You've set the itemCount attribute of the ListView with _ListFamilyPageState.data.body.family.length and you've use the index of it's builder with another list data.body.friends[index].id.toString()您已经使用 _ListFamilyPageState.data.body.family.length 设置了 ListView 的 itemCount 属性,并且您已经将其构建器的索引与另一个列表 data.body.friends[index].id.toString() 一起使用

I don't think both have the same number of elements我不认为两者具有相同数量的元素

The error is in your ListViewBuilder:错误在您的 ListViewBuilder 中:

ListView.builder(
  scrollDirection: Axis.vertical,
  shrinkWrap: true,
  itemCount: _ListFamilyPageState.data.body.family.length,
  itemBuilder: (context, index) {
    return Container(
        height: 74.0,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topRight: const Radius.circular(20.0),
              bottomRight: const Radius.circular(20.0)),
        ),
        width: MediaQuery.of(context).size.width - 50.0,
        child: Center(
            child: Text(
              data.body.friends[index].id.toString(),
              style:
              TextStyle(fontSize: 24.0),
            )));
  },
)

You specified that there are family.length items (in your data: 15), but you are pulling actual data from friends[index] (8 items in your data).您指定了family.length项目(在您的数据中:15),但您正在从family.length friends[index]中提取实际数据(您的数据中有 8 个项目)。

This gives you RangeError when rendering item at index 8.当在索引 8 处渲染项目时,这会给你 RangeError。

On top of that: you use static data in your state:最重要的是:您在您的状态中使用静态数据:

class _ListFamilyPageState extends State<ListFamily> {
  static Relations data;
  // ...
}

Don't do that.不要那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel - "RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel ItemList:RangeError(索引):无效值:有效值范围为空:0 - ItemList: RangeError (index): Invalid value: Valid value range is empty: 0 RangeError(索引):索引超出范围:没有有效的索引:0 - Flutter - RangeError (index): Index out of range: no indices are valid: 0 - Flutter Flutter 无效值:有效值范围为空:0 - Flutter Invalid value: Valid value range is empty: 0 RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效的状态代码: - RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 我收到 RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined - I am getting RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined 尝试在json中反序列化枚举时,索引值超出合法索引范围 - index value outside legal index range when trying to deserialize enum in json JSON.stringify 抛出 RangeError:巨大对象的无效字符串长度 - JSON.stringify throws RangeError: Invalid string length for huge objects “范围的坐标或尺寸无效” - “The coordinates or dimensions of the range are invalid” 索引2超出范围JSON - Index 2 out of range JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM