What is the difference between using the is-operator and using the run-time type in Dart

127 Views Asked by At

I have worked through the dart codelab for iterables and stumbled upon this code fragment:

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;
....

What is the difference between line (a) and line (b)? For me it seems like they do the same. Or more generally asked: What is the difference between using the is-operator and using the runtimeType property of an object for checking the run-time type in dart?

1

There are 1 best solutions below

1
Kevin Moore On BEST ANSWER

aSet is Iterable – this is true

aSet.runtimeType == Iterable this is false

So the is check handles subclasses.

Also, we really recommend that you avoid using runtimeType. Especially when compiling to JavaScript. It can really blow up your compiled app size.

I'll open an issue on that codelab!