oop - DART 中类字段的默认值
问题描述
我知道这是一个愚蠢的问题,但我已经坚持了很长一段时间了。
如何在 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在“默认参数值”部分。
推荐阅读
- html - Flex 项目在 Internet Explorer 11 中收缩太多
- r - 在R中以4的步长交替组合2个向量
- c++ - 特定数量的元素 - 向量
- swift - 如何在 Swift 中替换数组切片?
- javascript - img src 不能在 Vue js 中工作并且必须有正确的路径?
- github - 发布 GitHub 页面时触发 GitHub 操作
- python - vscode 语法高亮中断
- angular - 如何在 Angular Material 中使用 ckeditor5
- reactjs - 将 React Native 调试器与 React Native 应用程序 (expo) 集成时出错
- django - 如果我在 django 中删除图片 upload_to 会发生什么