简体   繁体   English

为什么 dart 编译器没有检测到这行代码中的错误? 变量类型显然是 boolean

[英]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 类型的最接近dynamicbool的公共基类型,它是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 .类似地,条件三元表达式的类型将是Stringdynamic最接近的公共基类型,它又将是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.

相关问题 dart + flutter:编译器为什么不识别类型不匹配 - dart + flutter: why doesn't the compiler recognize type mismatches 为什么私有变量没有在 dart 中初始化? - Why private variable doesn't initialize in dart? 为什么列表项不包含对Flutter中Dart中变量的引用? - Why doesn't the list item hold the reference to the variable in Dart in Flutter? Dart/Flutter 错误:“AuthSession”类型的值不能分配给“CognitoAuthSession”类型的变量 - Dart/Flutter Error: A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession' 获得“未来”<img> ' 不能在这个 Flutter/Dart 代码中分配给“Image”类型的变量? - Getting 'Future<Image>' can't be assigned to a variable of type 'Image' in this Flutter/Dart code? 为什么 CTRL + 单击 android 工作室中的一些 flutter 小部件不显示源 dart 代码? - Why CTRL + click on some flutter widgets in android studio doesn't show the source dart code? dart class 层次结构错误 - 'a' 不扩展 'b' 尝试使用是或是子类的类型 'b' - dart class hierarchy error - 'a' doesn't extend 'b' Try using a type that is or is a subclass of 'b' Stagehand 不支持 Dart 2.3.1 错误 - Stagehand doesn't support Dart 2.3.1 error 如果变量不是 Dart 中的一行代码中的 null,如何有条件地执行代码? - How to conditionally execute code if a variable is NOT null in one line of code in Dart? Dart 扩展 - 获取“不是类型”错误 - Dart extension - Getting "isn't a type" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM