[英]Why doesn't the dart compiler not detect an error in this line of code? The variable type is clearly a boolean
Look out for the comment written 'error is here'.注意写着“错误在这里”的评论。 The compiler should throw an error if the types don't match, but it doesn't.
如果类型不匹配,编译器应该抛出错误,但事实并非如此。 The variable type is a boolean and I have passed in a string as the value.
变量类型是 boolean,我传入了一个字符串作为值。 As such, their should be an error thrown that the types do not match.
因此,它们应该是抛出类型不匹配的错误。
class CompanyPreferences {
late bool editHistory;
late bool showOrderReference;
late bool canBeOffline;
late bool hasCancel;
late bool hasSkip;
late bool homepagePickup;
late double geofenceDistance;
late bool viewPendingOrders;
CompanyPreferences({
this.editHistory = true,
this.showOrderReference = false,
this.canBeOffline = false,
this.hasCancel = true,
this.hasSkip = true,
this.homepagePickup = false,
this.geofenceDistance = 0.0,
this.viewPendingOrders = false,
});
factory CompanyPreferences.fromMap(Map<String, dynamic> map) {
return CompanyPreferences(
editHistory: map["preference_data"]["driver_app"] == null ? true : map["preference_data"]["driver_app"]['edit_history'] ?? true,
showOrderReference: map["preference_data"]["driver_app"] == null ? false : map["preference_data"]["driver_app"]['show_order_reference'] ?? false,
canBeOffline: map["preference_data"]["driver_app"] == null ? false : map["preference_data"]["driver_app"]['can_be_offline'] ?? false,
hasCancel: map["preference_data"]["driver_app"] == null ? false : map["preference_data"]["driver_app"]['has_cancel'] ?? false,
hasSkip: map["preference_data"]["driver_app"] == null ? false : map["preference_data"]["driver_app"]['has_skip'] ?? false,
homepagePickup: map["preference_data"]["driver_app"] == null ? false : map["preference_data"]["driver_app"]['homepage_pickup'] ?? false,
geofenceDistance: map["preference_data"]["driver_app"] == null
? 0.0
: map["preference_data"]["driver_app"]['geofence_distance'] == null
? 0.0
: map["preference_data"]["driver_app"]['geofence_distance'].toDouble(),
// Error is here. The compiler doesn't throw an error.
viewPendingOrders: map["preference_data"]["driver_app"] == null ? "ERROR IS HERE" : map["preference_data"]["driver_app"]['view_pending_orders'] ?? false,
);
}
}
Distilling your problem down a bit:稍微提炼一下您的问题:
Type staticType<T>(T object) => T;
void main() {
var map = <String, dynamic>{};
var x = map['foo'] == null ? 'string' : map['bar'] ?? false;
print(staticType(x)); // Prints: dynamic
}
The problem is that map['bar']
has type dynamic
and basically poisons the rest of the conditional ternary expression.问题是
map['bar']
的类型为dynamic
并且基本上破坏了条件三元表达式的 rest 。
The "then" part of the conditional expression is of type String
, and the "else" part has a ??
条件表达式的“then”部分是
String
类型,“else”部分有一个??
expression.表达。 The result of that
??
结果
??
expression can either be the dynamic
type on the left side or the bool
type on the right. expression 可以是左边的
dynamic
类型,也可以是右边的bool
类型。 The ??
??
expression thus is determined to have a static type of the nearest common base type of dynamic
and bool
, which is dynamic
. expression 因此被确定为具有 static 类型的最接近
dynamic
和bool
的公共基类型,它是dynamic
。 Similarly, the type of the conditional ternary expression will be the nearest common base type of String
and dynamic
, which again will be dynamic
.类似地,条件三元表达式的类型将是
String
和dynamic
最接近的公共基类型,它又将是dynamic
。
Since the entire conditional ternary expression will be of type dynamic
, it will not be type-checked when passing it as a bool
argument.由于整个条件三元表达式的类型为
dynamic
,因此在将其作为bool
参数传递时不会进行类型检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.