如何将 Flutter & Dart List 与 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)),
];

1月和2月也在名单中。如何根据列表中添加的月份转换为Months类?

我必须上几个月(一月:真,二月:真(的房地产课。

json输出示例

"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)
}

相关内容

最新更新