使用Swiper控件的時候蛇更,更換Swiper數(shù)據(jù)源闺鲸,需要加入 key: ValueKey(listWidget.length),
避免在Swiper控件出現(xiàn)的問題筋讨。
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
以上方法可以解決,出現(xiàn)_positions.isNotEmpty的問題摸恍。順便解釋下解決方法的原理
static?bool?canUpdate(Widget?oldWidget,?Widget?newWidget) {
return?oldWidget.runtimeType?==?newWidget.runtimeType
&&?oldWidget.key?==?newWidget.key;
}
本身是一個抽象類悉罕,有一個工廠程構(gòu)造方法 ValueKey()赤屋。
直接子類主要有:LocalKey 跟 GlobalKey。
LocalKey 是增量算法的核心壁袄,決定哪個Element要保留类早,哪個Element要刪除。以下是LocalKey的三個子類嗜逻。
ValueKey:以值作為參數(shù)(數(shù)字涩僻、字符串);
ObjectKey:以對象作為參數(shù)栈顷;
UniqueKey:創(chuàng)建唯一標識逆日;
對應某一個? 或者 State 或者 。
參考文獻:https://baijiahao.baidu.com/s?id=1718959148055142446&wfr=spider&for=pc