使用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