如果参数为 null,则不设置 Dart 默认值
Dart default value not set if parameter is null
我对 dart 有点陌生,在使用参数的默认值时遇到了问题。我正在创建一个 class 并且在某些情况下参数可以为空。在那些情况下,我想应用默认值。因此,在下面的示例中,TargetField 的 fieldType 参数可以为 null,如果是这种情况,我想使用默认值。
我得到的错误是:
未处理的异常:
类型 'Null' 不是类型 'FieldType'
的子类型
我可以在调用方检查该值是否为空,然后传递默认值(评论 1),但我想在 TargetField class 中设置默认值(评论 2)。我还希望保持 fieldType 字段不可为空,因为它不应该为空。
感谢您的帮助。
enum FieldType {
string,
int,
date,
array,
lookup,
map
}
main() {
Map<String, Map> myMap = {
'target0': { 'type': FieldType.string},
'target1': { 'static': 'hello'},
'target2': { 'static': 'goodbye'},
'target3': { 'type': FieldType.date},
};
print('running now');
myMap.forEach((k, v) {
print('running now, $k : $v');
TargetField tf = TargetField(fieldName: k, fieldType: v['type']);
// Comment 1: Would like to avoid doing this, would be more comfortable doing
// something on the TargetField side to set the default value, not the caller.
// TargetField tf = TargetField(fieldName: k,
// fieldType: (v['type'] != null) ? v['type'] : FieldType.string);
tf.printType();
}
);
}
class TargetField {
FieldType fieldType;
final String fieldName;
TargetField({required this.fieldName, this.fieldType = FieldType.string}) {
//Comment 2: Can I do something here to set the value to the default value if the
//parameter passed is null?
}
printType() {
print('$fieldName type = ${fieldType.name}');
}
}
如果省略参数 或 null
,您可以使构造函数使用相同的默认值,方法是使默认参数为 null
并添加逻辑以落下返回到成员所需的默认值。请注意,构造参数可以为空,但成员不需要。例如:
class TargetField {
FieldType fieldType;
final String fieldName;
TargetField({required this.fieldName, FieldType? fieldType})
: fieldType = fieldType ?? FieldType.string;
此技术也适用于 。
我对 dart 有点陌生,在使用参数的默认值时遇到了问题。我正在创建一个 class 并且在某些情况下参数可以为空。在那些情况下,我想应用默认值。因此,在下面的示例中,TargetField 的 fieldType 参数可以为 null,如果是这种情况,我想使用默认值。
我得到的错误是: 未处理的异常: 类型 'Null' 不是类型 'FieldType'
的子类型我可以在调用方检查该值是否为空,然后传递默认值(评论 1),但我想在 TargetField class 中设置默认值(评论 2)。我还希望保持 fieldType 字段不可为空,因为它不应该为空。
感谢您的帮助。
enum FieldType {
string,
int,
date,
array,
lookup,
map
}
main() {
Map<String, Map> myMap = {
'target0': { 'type': FieldType.string},
'target1': { 'static': 'hello'},
'target2': { 'static': 'goodbye'},
'target3': { 'type': FieldType.date},
};
print('running now');
myMap.forEach((k, v) {
print('running now, $k : $v');
TargetField tf = TargetField(fieldName: k, fieldType: v['type']);
// Comment 1: Would like to avoid doing this, would be more comfortable doing
// something on the TargetField side to set the default value, not the caller.
// TargetField tf = TargetField(fieldName: k,
// fieldType: (v['type'] != null) ? v['type'] : FieldType.string);
tf.printType();
}
);
}
class TargetField {
FieldType fieldType;
final String fieldName;
TargetField({required this.fieldName, this.fieldType = FieldType.string}) {
//Comment 2: Can I do something here to set the value to the default value if the
//parameter passed is null?
}
printType() {
print('$fieldName type = ${fieldType.name}');
}
}
如果省略参数 或 null
,您可以使构造函数使用相同的默认值,方法是使默认参数为 null
并添加逻辑以落下返回到成员所需的默认值。请注意,构造参数可以为空,但成员不需要。例如:
class TargetField {
FieldType fieldType;
final String fieldName;
TargetField({required this.fieldName, FieldType? fieldType})
: fieldType = fieldType ?? FieldType.string;
此技术也适用于