【问题标题】:Flutter possible to create a List of AnimationController?Flutter可以创建一个AnimationController列表吗?
【发布时间】:2020-01-12 09:58:49
【问题描述】:

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

【问题讨论】:

  • 您的列表已经是空的,您需要添加AnimationController 才能使用任何索引。
  • 我在 initstate 中添加的不是吗?
  • 做 - dataCtrl.add(AnimationController( duration: Duration(milliseconds: 400))
  • @Hasen 不,您没有添加任何内容。看看我的回答

标签: list animation flutter dart


【解决方案1】:
@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,
  );
}

【讨论】:

  • 好的,我明白了。或者我猜dataCtrl.add(AnimationController(vsync: this, duration: Duration(milliseconds: 400))); 也可以?
  • 是的,你也可以这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 2018-07-18
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2019-11-28
相关资源
最近更新 更多