Home > Mobile >  Dart equivalent syntax
Dart equivalent syntax

Time:09-27

Dart Language

What is the equivalent operator for the "as" operator in type checking during runtime?

var foo = new Teacher();
var bar = new Student();
like (foo as Teacher);

Output

Instance ofTteacher

CodePudding user response:

The equivalent operator is "is" (foo is Teacher) should return true or false accordingly if foo is of type Teacher or otherwise.

CodePudding user response:

Key word as is used for type conversion.

If you want to check foo is Teacher or not:

print(foo is Teacher);
print(bar is Student);

If you just want to get Type of foo:

final fooType = foo.runtimeType;
final barType = bar.runtimeType;
print(fooType == barType);

Both fooType and barType is instance of Type.

  • Related