[英]ItemList: RangeError (index): Invalid value: Valid value range is empty: 0
[英]"RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel
imageList列表包含從 JSON 文件中提取的圖像路徑。 但是,此問題僅遇到一次,但在熱重載后消失。 Package 使用:carousel_slider:^3.0.0
代碼:
CarouselSlider(
options: CarouselOptions(
height: 250.0,
enlargeCenterPage: true,
autoPlay: true,
reverse: false,
enableInfiniteScroll: true,
autoPlayInterval: Duration(seconds: 3),
autoPlayAnimationDuration: Duration(milliseconds: 2000),
pauseAutoPlayOnTouch: true,
scrollDirection: Axis.horizontal,
),
items: imageList.map((img) {
return Builder(
builder: (BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 10.0
),
decoration: BoxDecoration(
color: Colors.transparent,
),
child: Image.asset(
img,
fit: BoxFit.fill,
),
);
},
);
}).toList(),
),
我猜你的 imageList 一開始是空的,當你填充它時,你沒有調用 setstate() 如果你是,這意味着輪播不允許你使用空列表添加一個 if 語句這將使它如果列表為空,則不會構建 carouselSlider
if(!imageList.isEmpty)
CarouselSlider()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.