简体   繁体   English

"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

The imageList list contains image paths extracted from a JSON file. imageList列表包含从 JSON 文件中提取的图像路径。 However, this issue is encountered only once but disappears after hot reload.但是,此问题仅遇到一次,但在热重载后消失。 Package used: carousel_slider: ^3.0.0 Package 使用:carousel_slider:^3.0.0

Code:代码:

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(),
                      ),

I'm guessing your imageList is empty at first and when you're filling it you're not calling setstate() and if you are it means the carousel doesn't let you use an empty list add an if statement this would make it so that the carouselSlider isn't built if the list is empty我猜你的 imageList 一开始是空的,当你填充它时,你没有调用 setstate() 如果你是,这意味着轮播不允许你使用空列表添加一个 if 语句这将使它如果列表为空,则不会构建 carouselSlider

if(!imageList.isEmpty)
  CarouselSlider()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM