首页 > 解决方案 > 在 Dart 中使用 is-operator 和使用运行时类型有什么区别

问题描述

我已经通过dart codelab 进行了迭代,并偶然发现了这个代码片段:

class EmailAddress {
  String address;

  EmailAddress(this.address);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
(a)          other is EmailAddress &&
(b)              runtimeType == other.runtimeType &&
                 address == other.address;
....

(a) 行和 (b) 行有什么区别?对我来说,他们似乎也在做同样的事情。或者更一般地问:使用 is-operator 和使用对象的 runtimeType 属性来检查 dart 中的运行时类型有什么区别?

标签: darttypecheckingruntime-type

解决方案


aSet is Iterable- 这是true

aSet.runtimeType == Iterable这是false

所以是检查处理子类。

此外,我们真的建议您避免使用runtimeType. 特别是在编译为 JavaScript 时。它真的会炸毁你编译的应用程序的大小。

我将在那个 codelab 上打开一个问题!


推荐阅读