List 颤振是否可以创建AnimationController列表?

List 颤振是否可以创建AnimationController列表?,list,animation,flutter,dart,List,Animation,Flutter,Dart,不确定为什么不起作用,它抛出错误RangeError(index):无效值:有效值范围为空:0 class _HomePageState extends State<HomePage> with TickerProviderStateMixin { List<AnimationController> dataCtrl = List<AnimationController>(); @override void initState() { su

不确定为什么不起作用,它抛出错误
RangeError(index):无效值:有效值范围为空:0

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {

List<AnimationController> dataCtrl = List<AnimationController>();

  @override
  void initState() {
    super.initState();
    dataCtrl = [];
    dataCtrl[0] = AnimationController(
      duration: Duration(milliseconds: 400),
      vsync: this,
    );
  }
class\u HomePageState使用TickerProviderStateMixin扩展状态{
List dataCtrl=List();
@凌驾
void initState(){
super.initState();
dataCtrl=[];
dataCtrl[0]=动画控制器(
持续时间:持续时间(毫秒:400),
vsync:这个,,
);
}

您的列表已为空,您需要将
AnimationController
添加到列表中,然后才能对其使用任何索引。我正在initstate中添加它,不是吗?do-
dataCtrl.add(AnimationController(持续时间:持续时间(毫秒:400))
@Hasen No您没有添加任何内容。请查看我的答案您的列表已为空,您需要将
AnimationController
添加到列表中,然后才能对其使用任何索引。我将在initstate中添加它,不是吗?do-
dataCtrl.add(AnimationController(持续时间:持续时间(毫秒:400))
@Hasen不,你没有添加任何内容。请查看我的回答。我明白了。或者我猜只要
dataCtrl.add(AnimationController(vsync:this,duration:duration(毫秒:400));
也可以。是的,你也可以这样做。我明白了。或者我猜只要
dataCtrl.add(AnimationController(vsync:this,duration:duration(毫秒:400))就可以了));
也可以吗?是的,你也可以这样做。
@override
void initState() {
  super.initState();

  // add some AnimationController object before using any index
  dataCtrl.add(AnimationController(vsync: this, duration: Duration(seconds: 1)));

  // now it is Ok to use 0 index
  dataCtrl[0] = AnimationController(
    duration: Duration(milliseconds: 400),
    vsync: this,
  );
}