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

Flutter使用ListView展示图片报错Not in inclusive range 0..1: -1如何修复

问题原因

报错是数组下标越界导致的,ListView.builder的itemBuilder回调返回的index从0开始计数,而你在加载图片的代码里使用了keepImage[index - 1]取值,当渲染第一个元素时index=0,计算得到的下标为-1,不在数组合法的下标范围0~keepImage.length-1内,所以触发该错误。

修复方案

只需要修改图片资源取值的下标即可,将index - 1改为index:

// 修改前代码
image: DecorationImage(
    image: FileImage(keepImage[index - 1]),
    fit: BoxFit.cover
)

// 修改后代码
image: DecorationImage(
    image: FileImage(keepImage[index]),
    fit: BoxFit.cover
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 17:45:03