不知道为什么这不起作用,它会引发错误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,
    );
  }

最佳答案

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

09-11 19:36