我知道这是一个愚蠢的问题,但是我已经坚持了很长时间。
如何在DART中设置类成员的默认值。
这是我提供成员默认值的方式,但始终为 null 。
如果在构造函数中提供了这些值,则应使用这些值,否则应使用默认值。
class BillingInfoDetails {
bool billToClient = false;
String clientName = "";
String reasonOfTravel = "";
String remarks = "";
BillingInfoDetails({
this.billToClient,
this.clientName,
this.reasonOfTravel,
this.remarks,
});
}
最佳答案
像这样:
class BillingInfoDetails {
bool billToClient;
String clientName;
String reasonOfTravel;
String remarks;
BillingInfoDetails({
this.billToClient = false,
this.clientName = "",
this.reasonOfTravel = "",
this.remarks = "",
});
}
您可以在《 Dart旅游指南:https://dart.dev/guides/language/language-tour#parameters》的“默认参数值”部分中找到此信息。
关于oop - DART中类字段的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64336694/