Flutter 解决在使用Swiper过程中出现ScrollController not attached to any scroll views.异常

Flutter Swiper是一个轮播图组件,内部包含一个Widget List,当这个Widget List数量发生变化的时候如果出现类似这种异常情况导致轮播图不滑动或者其他红屏等错误,

I/flutter (15202): ScrollController not attached to any scroll views.
I/flutter (15202): 'package:flutter/src/widgets/scroll_controller.dart':
I/flutter (15202): Failed assertion: line 110 pos 12: '_positions.isNotEmpty'

解决办法:给Swiper加一个LocalKey即可解决,我这里加了个UniqueKey,属于一个LocalKey

child: Swiper(
          key: UniqueKey(),
          autoplay: true,
          loop: true,
          itemBuilder: (BuildContext context, int index) {....

具体的原因可以参考这篇博文,理解key值在flutter渲染刷新时候的作用  https://www.jianshu.com/p/6e704112dc67

你可能感兴趣的:(Flutter开发笔记)