可能创建一个AnimationController列表吗?

时间:2019-09-10 15:47:42

标签: list animation flutter dart

不知道为什么这不起作用,它会引发错误RangeError (index): Invalid value: Valid value range is empty: 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,
    );
  }

1 个答案:

答案 0 :(得分:0)

@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,
  );
}