You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter调用接口读取JSON数组时触发RangeError索引无效报错如何解决

错误原因

你触发索引越界报错的核心问题是代码错误复用了ListView.builder的循环索引index访问两层结构完全不同的数组:

  • 你提供的JSON结构中standings数组长度仅为1,只有standings[0]是合法的访问路径
  • 你把循环生成的index同时用来访问standings的索引和table的索引,当index >=1时,standings[index]就会触发越界,和你收到的报错提示完全吻合。

修复方案

把代码中所有data!.standings[index]都修改为data!.standings[0]即可,仅用循环的index访问table数组的元素。
修复后的关键代码段如下:

itemBuilder: (context, index) {
  return Card(
    child: ListTile(
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text("${data!.standings[0].table[index].position}"),
          CircleAvatar(
            child: SvgPicture.network(
                "${data.standings[0].table[index].team.crestUrl}"),
          ),
          Text("${data.standings[0].table[index].team.name}")
        ],
      ),
    ),
  );
}

内容的提问来源于stack exchange,提问作者Subhist Subedar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 05:51:01