使用Swiper控件的时候,更换Swiper数据源,需要加入 key: ValueKey(listWidget.length),
避免在Swiper控件出现的问题。
flutter: ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
flutter: The following assertion was thrown while dispatching notifications for SwiperController:
flutter: ScrollController not attached to any scroll views.
flutter: 'package:flutter/src/widgets/scroll_controller.dart':
package:flutter/…/widgets/scroll_controller.dart:1
flutter: Failed assertion: line 107 pos 12: '_positions.isNotEmpty'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter: https://github.com/flutter/flutter/issues/new?template=2_bug.md
以上方法可以解决,出现_positions.isNotEmpty的问题。顺便解释下解决方法的原理
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType
&& oldWidget.key == newWidget.key;
}
本身是一个抽象类,有一个工厂程构造方法 ValueKey()。
直接子类主要有:LocalKey 跟 GlobalKey。
LocalKey 是增量算法的核心,决定哪个Element要保留,哪个Element要删除。以下是LocalKey的三个子类。
ValueKey:以值作为参数(数字、字符串);
ObjectKey:以对象作为参数;
UniqueKey:创建唯一标识;
对应某一个 或者 State 或者 。
参考文献:https://baijiahao.baidu.com/s?id=1718959148055142446&wfr=spider&for=pc