Flutter 错误 The provided ScrollController is currently attached to more than one ScrollPosition

8 min read

一个页面里有多个 ListView 时,不添加 controller 时会提示如下错误:

======== Exception caught by animation library ================================

The following assertion was thrown while notifying status listeners for AnimationController:
The provided ScrollController is currently attached to more than one ScrollPosition.

The Scrollbar requires a single ScrollPosition in order to be painted.

When the scrollbar is interactive, the associated Scrollable widgets must have unique ScrollControllers. The provided ScrollController must be unique to a Scrollable widget.

...

The AnimationController notifying status listeners was: AnimationController#42be8( 1.000)

=====================================================================

需要为 ListView 设置 controller 属性:

ListView.builder(
  controller: ScrollController(),
  scrollDirection: Axis.vertical,
  itemCount: data.length,
  itemBuilder: (_, index) => Container(
    child: Text(data[index].exInfo)
  ),
),

controller属性:

controller: ScrollController(),