简体   繁体   中英

"RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel

The imageList list contains image paths extracted from a JSON file. However, this issue is encountered only once but disappears after hot reload. Package used: 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

if(!imageList.isEmpty)
  CarouselSlider()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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