我正在监听一个PageController以获取位置信息,然后将其与ListView同步。当PageView被操作时,ListView同时被操作。
示例:https://github.com/Ramotion/cardslider-android
然而,在v0.6.0之后,我会得到一个断言错误,指出我的ScrollController未附加到任何视图上。每次有流事件触发.jumpTo()
方法时都会引发此错误。它仍然可以工作,但是这个断言错误让我感到疯狂。
[VERBOSE-2:shell.cc(181)] Dart Error: Unhandled exception:
'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 169 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.
#0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
#1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
#2 ScrollController.jumpTo (package:flutter/src/widgets/scroll_controller.dart:169:12)
#3 MyTitle.build.<anonymous closure> (file:///Users/lukepighetti/code/when-coin/when_coin_2/lib/screens/rate/widgets/title.dart:19:19)
#4 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#5 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#6 _DelayedData.perform (dart:async/stream_impl.dart:584:14)
#7 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:700:11)
#8 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:660:7)
#9 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#10 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
如何在不遇到异常情况的情况下使用ScrollController.jumpTo()
函数?
class MyTitle extends StatelessWidget {
final List<Category> categories;
MyTitle({this.categories});
@override
Widget build(BuildContext context) {
final _controller = ScrollController();
double height = 36.0;
// double width = MediaQuery.of(context).size.width * 0.8;
BlocProvider.of(context).page.listen((page) {
_controller.jumpTo(height * page);
});
return Container(
height: height,
// width: width,
child: ListView(
controller: _controller,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
children: categories
.map((c) => _Title(
title: c.title,
index: categories.indexOf(c),
))
.toList(),
),
);
}
}
Scrollbar
而不是SingleChildScrollView``child: Scrollbar( child: SingleChildScrollView( controller: _scrollController,
。 我想分享这个错误以防您们中的任何人遇到类似的情况。 - Adrian Moisa