![](/img/trans.png)
[英]Flutter Dart: how can I change the value of a class property in a list of Classes
[英]How can I combine Flutter & Dart List with Class Property?
class MonthModel {
String? title;
Months? months;
MonthModel({this.title, this.months});
}
class Months {
bool? january;
bool? february;
bool? march;
bool? april;
Months({this.january, this.february, this.march, this.april});
}
List<MonthModel> selectedMonths = [
MonthModel(title: "January", months: Months(january: true)),
MonthModel(title: "February", months: Months(february: true)),
];
一月和二月包含在列表中。 如何根据列表中添加的月份转换为月份 class?
我必须拥有几个月(一月:真,二月:真)的财产 class。
示例 json output
"months" : {
"january": true,
"february": true
}
我想你想要类似的东西
void main() {
List<MonthModel> selectedMonths = [
MonthModel(title: "January", months: Months(january: true)),
MonthModel(title: "February", months: Months(february: true)),
];
Months months = Months();
for (final m in selectedMonths.map((e) => e.months).toList()) {
if (m != null && m.january == true) {
months.january = true;
}
if (m != null && m.february == true) {
months.february = true;
}
if (m != null && m.march == true) {
months.march = true;
}
if (m != null && m.april == true) {
months.april = true;
}
}
print(months.toString());
///Months(january: true, february: true, march: null, april: null)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.