我有一個 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)})
}
當我初始化該類時,我收到一條錯誤訊息,提示“可選引數的默認值必須是常量”。基于這篇文章,我嘗試了以下方法:
Reminder({this.name = "", this.type = "", DateTime? startDate,
this.repeatFrequency = "", DateTime? endDate}) :
this.startDate = (startDate != null ? startDate : DateTime.utc(2021,1,1))
這種方法現在似乎有效,但是我只能為 startDate 而不是 endDate 這樣做。有沒有辦法用這種方法初始化兩個變數?
uj5u.com熱心網友回復:
您可以嘗試使用以下代碼:
Reminder({this.name = "", this.type = "", DateTime? startDate,
this.repeatFrequency = "", DateTime? endDate}) :
this.startDate = (startDate != null ? startDate : DateTime.utc(2021,1,1)),
this.endDate = endDate ?? DateTime.utc(2021,1,1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331254.html
