flutter项目遇到的问题
程序员文章站
2022-06-01 14:11:29
...
20-04-23
- model类的一个属性是数组, 修改这个数组的时候(添加元素), 当前修改成功, 但下次取值的时候还是原来的值
_questionInfo = InquiryQuestionModel();
_questionInfo.questionContent = [QuestionAnswerModel(value: 0)];
class InquiryQuestionModel {
String questionName;
int depart;
int questionType; // 1单选 2多选 3简答
List<InquiryAnswerModel> questionContent; // 答案列表
InquiryQuestionModel({this.questionName, this.depart, this.questionType, this.questionContent});
}
class InquiryAnswerModel {
String content; // 答案内容
int value; // 答案标识
InquiryAnswerModel({this.content, this.value});
}
List<Widget> _anwser () {
int len = _questionInfo.questionContent;
print('-------start--------len ${len}--------------------');
List<Widget> anwsers = [];
for (int i = 0; i < len; i++) {
anwsers.add(
Container(
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(...),
),
Offstage(
offstage: i == len - 1,
child: GestureDetector(...),
),
Offstage(
offstage: i != _list.length - 1,
child: GestureDetector(
onTap: () {
setState(() {
_list.add(InquiryAnswerModel(
value: i + 1
));
});
print('add之后的长度${_questionInfo.questionContent.length},');
print('最后一个元素 ${_questionInfo.questionContent[_questionInfo.questionContent- 1]}')
},
child: Icon(Icons.add, color: Color(0xFF54D3C0)),
),
)
],
),
)
);
}
return anwsers;
}
打印结果: