gpt4 book ai didi

flutter - ScrollController.jumpTo() "ScrollController not attached to any scroll views"

转载 作者:IT王子 更新时间:2023-10-29 06:34:34 32 4
gpt4 key购买 nike

我正在监听 PageController 以获取位置,然后将其与 ListView 同步。当操作 PageView 时,同时操作 ListView。

示例:https://github.com/Ramotion/cardslider-android

但是,在 v0.6.0 之后,我收到一个断言错误,指出我的 ScrollController 未附加到任何 View 。每次有流事件触发 .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(),
),
);
}
}

最佳答案

正如我上面的回答所述,当 ScrollController 尚未附加到 ListView 或其他 ScrollView 时,您正在使用它。您可以使用 hasClients 属性进行检查。

if (_scrollController.hasClients) {
await _scrollController.animateTo(
0.0,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
}

关于flutter - ScrollController.jumpTo() "ScrollController not attached to any scroll views",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52296387/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com