flutter_swiper:Another exception was thrown: ScrollController attached to multiple scroll views.

Another exception was thrown: ScrollController attached to multiple scroll views.

翻译一下:引发了另一个异常:ScrollController连接到多个滚动视图。

Flutter Swiper是一个轮播图组件,内部包含一个Widget List,当这个Widget List数量大于1,就可能会有这种情况

解决方案:给Swiper加一个Key即可解决

原有代码

 return Container(
      child: AspectRatio(
        aspectRatio: 1.5 / 1, // 宽高比450/300
        child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            return new Image.network(
              imgList[index]['url'],
              fit: BoxFit.fill,
            );
          },
          itemCount: imgList.length,
          pagination: new SwiperPagination(),
          control: new SwiperControl(),
          autoplay: true,
        ),
      ),
    );

更新代码

 return Container(
      child: AspectRatio(
        aspectRatio: 1.5 / 1, // 宽高比450/300
        child: Swiper(
          key: UniqueKey(), // 这个必须添加,代表唯一
          itemBuilder: (BuildContext context, int index) {
            return new Image.network(
              imgList[index]['url'],
              fit: BoxFit.fill,
            );
          },
          itemCount: imgList.length,
          pagination: new SwiperPagination(),
          control: new SwiperControl(),
          autoplay: true,
        ),
      ),
    );

大功告成!!

posted @   糖~豆豆  阅读(424)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
Live2D
欢迎阅读『flutter_swiper:Another exception was thrown: ScrollController attached to multiple scroll views.』
点击右上角即可分享
微信分享提示