Flutter 解决SwiperController: ScrollController not attached to any scroll views.

川长思鸟来 2023-09-29 18:38 86阅读 0赞

一、错误信息

  1. The following assertion was thrown while dispatching
  2. notifications for SwiperController:
  3. ScrollController not attached to any scroll views.
  4. 'package:flutter/src/widgets/scroll_controller.dart':
  5. Failed assertion: line 107 pos 12: '_positions.isNotEmpty'

bae162c04b5f983e724d888dda7fbc1b.png

二、原因分析

Flutter Swiper是一个轮播图组件,内容有一个下拉刷新的控件。当下拉刷新轮播图数量发生变化并且轮播图在最后一个视图的情况下。就会出现类似这种异常情况导致轮播图不滑动或者其他红屏等错误。

如下图:

4a3ad61ea4b765ce20e306fb841dfb71.png

三、解决方案

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

四、扩展知识。

理解key值在flutter渲染刷新时候的作用

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读