为什么元素使用旧索引而不是更新索引来调用函数?

问题描述 投票:0回答:1

在我的代码中,我正在做与此类似的事情,

List widgetList = List.generate(
  3,
  (index) => Container(
    color: getColor(index),
  ),
);

// Now updating the element of List.

temp = widgetList.removeLast();
widgetList.insertAt(0, temp);

// Now after the update, the widget which is now at index 0 is still calling
// getColor() function with its old index 3 i.e. the index with which it was created.

我该如何解决这个问题?在我的代码中,我必须更改stackChildernList的顺序才能获得所需的动画。

flutter dart flutter-layout flutter-dependencies flutter-animation
1个回答
0
投票

此行为绝对正常,请考虑一下,当使用构造函数List.generate时,您正在传递一个立即生成它的函数!使用构造函数中传递的旧函数不会在列表中添加或删除或更改元素!

如果不是在升级sdk之前的行为,那么在旧版本中应该是一个小故障。

© www.soinside.com 2019 - 2024. All rights reserved.