如何解决Flutter mobx 观察者不在 List.firstWhere 上工作
嗨,我想更新我的模型列表。
我的模型;
class Question {
String id;
String title;
String questionInfo;
String questionInfoSub;
List<Answers> answers;
}
我的观点;
ListView buildListView(ServiceStore _serviceStore,context) {
return ListView.builder(
itemCount: _serviceStore.answeRSSummaryList.length,itemBuilder: (context,index) {
return Observer(builder: (context) {
return ReadOnlyWidget(
question: _serviceStore.answeRSSummaryList[index],editIcon: true,onPress: () {
showModalBottomSheetSummary(
context,_serviceStore.answeRSSummaryList[index]);
},);
});
});
}
我正在使用动作函数更新 showModalBottomSheetSummary 中的对象;
'''
@observable
ObservableList<Question> _answeRSSummaryList = new ObservableList();
@action
updateAnswer(Question question) {
//I am making false my prevIoUsly chosen answer
_answeRSSummaryList
.firstWhere((item) => item.id == question.id)
.answers
.firstWhere((element) => element.isSelected == true)
.isSelected = false;
//I m making true what i want answer
_answeRSSummaryList
.firstWhere((item) => item.id == question.id)
.answers
.firstWhere((element) => element.id == _answerId)
.isSelected = true;
//Checking if it has changed here.it is changing
_answeRSSummaryList.forEach((element) {.
element.answers.forEach((element) {
print(element.title);
print(element.isSelected);
});
}
如果我对列表使用 .remove 函数。它正在与观察者一起在屏幕上运行和删除,但是当我用第一个 Where 更改我的答案时。它不是在屏幕上改变而是在后面改变。
解决方法
我们知道 mobX 不适用于常规 List,我们有 ObservableList。
并且您的问题模型有答案列表,因此它不会做出反应。我在 mobX 中的嵌套列表有类似的问题。
这是一个演示解决方案示例。
QuestionListStore 有一个 QuestionStore 列表。而且我们知道 QuestionStore 有 ObserverableList,它变得反应性。
class QuestionListStore = _QuestionListStore with _$QuestionListStore;
abstract class _QuestionListStore with Store {
@observable
ObservableList<QuestionStore> questionList =
ObservableList<QuestionStore>();
@action
void addQuestion(Question question){
QuestionStore questionStore = QuestionStore(
id: question.id,title: question.title,);
questionList.add(questionStore);
// you can access QuestionStore methods here
questionStore.addAnswer(// instance of Answer model);
}
}
class QuestionStore = _QuestionStore with _$QuestionStore;
abstract class _QuestionStore extends Question with Store {
_QuestionStore ({
id,title,}) : super(id: id,title: title)
@observable
ObservableList<Answer> answerList = ObservableList<Answer>();
@action
void addAnswer(Answer answer){
answerList.add(answer);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。