【发布时间】:2021-04-16 13:55:34
【问题描述】:
我有以下状态,想在构造函数中设置 DateTime 的默认值。我想将当前日期作为默认日期,但我不确定这是否可能,因为它需要保持不变。DateTime.now() 函数或例如 DateTime.utc(1989, 11, 9) 也不起作用,但还有其他方法可以分配默认日期时间吗?
class CreateDidState {
final String firstname;
final String lastName;
final String email;
final String phoneNumber;
final DateTime dateOfBirth;
final String sex;
final String address;
final String city;
final String state;
final String postalCode;
final String country;
CreateDidState(
{this.firstname = "",
this.lastName = "",
this.email = "",
this.phoneNumber = "",
this.dateOfBirth = DateTime.now(), //The default value of an optional parameter must be constant.
this.sex = "",
this.address = "",
this.city = "",
this.state = "",
this.postalCode = "",
this.country = ""});
}
【问题讨论】: