【发布时间】:2021-10-20 01:35:02
【问题描述】:
我有一个 Reminder 类,它有两个日期变量 - startDate 和 endDate。
class Reminder {
final String name;
final String type;
final DateTime startDate;
final String repeatFrequency;
final DateTime endDate;
Reminder({this.name = "", this.type = "", DateTime startDate = DateTime.utc(2021,1,1),
this.repeatFrequency = "", DateTime endDate = DateTime.utc(2021,1,1)})
}
当我初始化类时,我收到一条错误消息“可选参数的默认值必须是常量”。基于this article,我尝试了以下方法:
Reminder({this.name = "", this.type = "", DateTime? startDate,
this.repeatFrequency = "", DateTime? endDate}) :
this.startDate = (startDate != null ? startDate : DateTime.utc(2021,1,1))
这种方法现在似乎有效,但是我只能对 startDate 而不是 endDate 这样做。有没有办法用这种方法初始化两个变量?
【问题讨论】: