首页 > 解决方案 > 颤振力为列表中的所有对象设置属性值

问题描述

我有一个包含自定义对象的列表,并希望对其进行迭代并将值设置为 true/false。

看到了setAll方法,但就像我不只更改列表中每个对象的属性一样。

我的对象看起来像:

class Feature {
  String id;
  String title;
  String subtitle;
  IconData leftIcon;
  IconData topRightIcon;
  IconData bottomRightIcon;
  bool isEnable;
  Feature({this.title, this.subtitle, this.leftIcon, this.topRightIcon, this.bottomRightIcon, this.isEnable,this.id});
}

所以想要迭代的对象是:List<Feature> features;

在颤动中怎么可能?

标签: flutterdart

解决方案


假设您要为 a 中的每个设置为,您isEnable可以执行以下操作:falseFeatureList<Feature>

features.forEach((f) => f.isEnable = false);

或者

for (final f in features) {
  f.isEnable = false;
}

推荐阅读