ChatGPT解决这个技术问题 Extra ChatGPT

How to perform runtime type checking in Dart?

Dart specification states:

Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages).

Sounds great, but there is no instanceof-like operator. So how do we perform runtime type-checking in Dart? Is it possible at all?


G
Günter Zöchbauer

The instanceof-operator is called is in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now seems to be http://www.dartlang.org/articles/optional-types/.

Here's an example:

class Foo { }

main() {
  var foo = new Foo();
  if (foo is Foo) {
    print("it's a foo!");
  }
}

Looks like there is no mention of is operator at all in the specification. It's better to refere to the grammar file in Dart sources: code.google.com/p/dart/source/browse/trunk/dart/language/…
@Idolon, the is operator is defined on page 59 of the spec, section 10.30 'Type test'
is and is! can be found in the Operators section of the Dart language tour.
new syntax is getTypeName(dynamic obj) => obj.runtimeType;
!= but is!...confuses me not it does
s
sbedulin

Dart Object type has a runtimeType instance member (source is from dart-sdk v1.14, don't know if it was available earlier)

class Object {
  //...
  external Type get runtimeType;
}

Usage:

Object o = 'foo';
assert(o.runtimeType == String);

RuntimeType is only for debugging purposes and the application code shouldn't depend on it. It can be overridden by classes to return fake values and probably returns unusable values when transpiled to JS
Thanks for your remark, I'm pretty new to Dart, and I agree that runtimeType may be overriden by classes, although I can't think of a reason why they would. (external code can't set the value sinse it's a getter) Personally, I would stick to is and reflection.
It's fine this is mentioned here. It's not very obvious that runtimeType has these limitations.
@GünterZöchbauer comment is no longer true in Dart 2. It should be fine to use it now.
I'm not sure why he thinks it's ok in Dart 2. Perhaps I missed something.
R
Rob

As others have mentioned, Dart's is operator is the equivalent of Javascript's instanceof operator. However, I haven't found a direct analogue of the typeof operator in Dart.

Thankfully the dart:mirrors reflection API has recently been added to the SDK, and is now available for download in the latest Editor+SDK package. Here's a short demo:

import 'dart:mirrors'; 

getTypeName(dynamic obj) {
  return reflect(obj).type.reflectedType.toString();
}

void main() {
  var val = "\"Dart is dynamically typed (with optional type annotations.)\"";
  if (val is String) {
    print("The value is a String, but I needed "
        "to check with an explicit condition.");
  }
  var typeName = getTypeName(val);
  print("\nThe mirrored type of the value is $typeName.");
}

it is good solution but, we have error: Unsupported operation: dart:mirrors is no longer supported for web apps
@Lii This answer was written for Ecma TC52. See dart.dev/faq
Be aware that Flutter, if you're using that, disables reflection (because it breaks tree shaking).
D
Duncan

There are two operators for type testing: E is T tests for E an instance of type T while E is! T tests for E not an instance of type T.

Note that E is Object is always true, and null is T is always false unless T===Object.


Could you explain what is meant by by T===Object? Dart doesn't have the triple equals operator, but you chose to use it rather than double equals, so I assume the difference has significance.
@MattC That was written more than 7 years ago! I think what I meant was null is Object would be true but null is T false for any other type T. tbh though I haven't been near Dart for many years now so can't be certain.
You save my day man! With this: var isAuthFailure = Failure is! AuthenticationFailure;
M
Maxim Saplin

Exact type matching is done via runtimeType property. Checking if an instance or any of its parent types (in the inheritance chain) is of the given type is done via is operator:

class xxx {}

class yyy extends xxx {}

void main() {
  var y = yyy();
  
  print(y is xxx);
  print(y.runtimeType == xxx);
}

Returns:

true
false

2
2 revs, 2 users 89%

Just to clarify a bit the difference between is and runtimeType. As someone said already (and this was tested with Dart V2+) the following code:

class Foo {
  @override
  Type get runtimeType => String;
}
main() {
  var foo = Foo();
  if (foo is Foo) {
    print("it's a foo!");
  }
  print("type is ${foo.runtimeType}");
  
}

will output:

it's a foo! 
type is String

Which is wrong. Now, I can't see the reason why one should do such a thing...


A
Alish Giri

Simply use .runtimeType on the property like below,

print(unknownDataTypeProperty.runtimeType)

l
lava

T is The type

   print( T.runtimeType)

https://i.stack.imgur.com/aFx70.png

https://i.stack.imgur.com/rQRGh.png


K
Kolawole Elijah

print("enter your phone number:\n"); var phone number = stdin.readLineSync();

if(phone number.runtimeType is int == true)     // checks if the values input are integers
{
print('you have successfully input your phone number!');
}
else{
print('only numbers are allowed');
}

As with most programming language, you don't need to add == true, the expression itself resolves to a Boolean. You just need if (thing is int) - and then you see that your solution is actually the same as the accepted answer.
Phone numbers aren't actually numbers. They may begin with a + sign, they may contain leading zeros that are important, they may contain spaces... and your code does not compile, since you managed to sneak a space into your variable name.