gpt4 book ai didi

list - 如何为列表中的单个项目(而不是整个列表)编写 setter?

转载 作者:IT王子 更新时间:2023-10-29 07:02:57 24 4
gpt4 key购买 nike

我的模型中有一个包含 bool 列表的类。在我的 UI 中,我想通过 setter 在列表中的单个项目中设置 bool 状态(这样我也可以保存它)。我无法弄清楚语法(或者这是否是一件有效的事情)。

   ///This is OK
set notificationDismissed(bool notificationDismissed){
_notificationDismissed = notificationDismissed;
saveParameterBoolean(_notificationDismissedKey,
_notificationDismissed);
}
bool get notificationDismissed => _notificationDismissed;


///This is OK too
List<bool> get questionsAnswered => _questionsAnswered;
set questionsAnswered(List<bool> questionsAnswered){
_questionsAnswered = questionsAnswered;
for(int i=0; i<_questionAnsweredParamKeys.length; i++ ){
saveParameterBoolean(_questionAnsweredParamKeys[i],
_questionsAnswered[i]);
}
updateState();
}

///This is not OK !!!! but should show what I want to do
List<bool> get questionsAnswered[index] => _questionsAnswered[index];
set questionsAnswered[index](bool questionsAnswered[index]){
_questionsAnswered[index] = questionsAnswered[index];
saveParameterBoolean(_questionAnsweredParamKeys[index],
_questionsAnswered[index]);
updateState();
}

我知道我在这里遗漏了一些明显的东西,非常感谢任何帮助

最佳答案

getset 函数不能接受任何参数。最简单的方法是使用普通函数:

bool getQuestionsAnswered(int index) =>  _questionsAnswered[index];

void setQuestionsAnswered(int index, bool value) {
_questionsAnswered[index] = value;
saveParameterBoolean(_questionAnsweredParamKeys[index], _questionsAnswered[index]);
updateState();
}

另一种选择是将 _questionsAnsweredList 更改为实现 operator [] 的自定义类(以获取元素)和operator []=(设置一个元素),然后你可以让他们做任何你想做的事情。

关于list - 如何为列表中的单个项目(而不是整个列表)编写 setter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56612316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com