颤动:widget.controller!= null:不正确

时间:2020-10-20 01:24:17

标签: flutter

当我使用小部件Scrollbar向我的ListView添加滚动条时,尽管布局正常,但仍抛出了该断言。完整的信息和相关代码如下:

══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═
I/flutter ( 4453): The following assertion was thrown during a scheduler callback:
I/flutter ( 4453): 'package:flutter/src/material/scrollbar.dart': Failed assertion: line 111 pos 20: 'widget.controller
I/flutter ( 4453): != null': is not true.
I/flutter ( 4453):
I/flutter ( 4453): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 4453): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 4453): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 4453):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 4453):
I/flutter ( 4453): When the exception was thrown, this was the stack:
I/flutter ( 4453): #2      _ScrollbarState.didChangeDependencies.<anonymous closure> (package:flutter/src/material/scrollbar.dart:111:20)
I/flutter ( 4453): #3      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
I/flutter ( 4453): #4      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1060:9)
I/flutter ( 4453): #5      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:968:5)
I/flutter ( 4453): #9      _invoke (dart:ui/hooks.dart:261:10)
I/flutter ( 4453): #10     _drawFrame (dart:ui/hooks.dart:219:3)
I/flutter ( 4453): (elided 5 frames from class _AssertionError and dart:async)
I/flutter ( 4453): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 4453): Another exception was thrown: 'package:flutter/src/material/scrollbar.dart': Failed assertion: line 111 pos 20: 'widget.controller !=
 null': is not true.
Scrollbar(
                          isAlwaysShown: true,
                          child: ListView(
                            controller: sc,
                            scrollDirection: Axis.vertical,
                            padding: EdgeInsets.fromLTRB(ScreenUtil.dp(22), 0, ScreenUtil.dp(23), ScreenUtil.dp(8.13)),
                            children: <Widget>[
                              Html(data: """$html""",),
                            ],
                          ),
                        ),
                      ),

1 个答案:

答案 0 :(得分:1)

堆栈跟踪中的错误显示为:

'package:flutter / src / material / scrollbar.dart':失败的断言:第111行pos 20:'widget.controller I / flutter(4453):!= null'

因此,基本上Scrollbar小部件找不到与其连接的任何ScrollController

如果您转到Scrollbarhere)小部件类的官方文档,则表明如果isAlwaysShownFlaghere)设置为{{1 }},您需要向true小部件提供ScrollController

指示滚动条是否应该始终可见。 如果为false,则滚动条将在滚动过程中显示,否则将逐渐消失。 设置为true时,滚动条将始终可见并且永不淡出。 在这种情况下,必须设置控制器属性。应该将其传递给相关的Scrollable的ScrollController。

也尝试将相同的Scrollbar添加到ScrollController小部件中,这样可以解决您的问题。

因此您的小部件应如下所示:

Scrollbar