Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart see if Instance of type inherits from other type

Tags:

dart

class a {/*code*/}
class b extends a {/*more code*/}
void main() {
  b c = new b();
  print(c.runtimeType == a); //type == inheritedType
  //or
  print(c.runtimeType inherits a); //operator for inheritance
  //or
  print(c inherits a); //other operator for inheritance
}

Can you use type == inheriting type, type inherits other type, or instance inherits other type or type? Is there even a way to do such thing? Because I have not found any way yet.

like image 298
Alex-c Avatar asked Dec 13 '16 18:12

Alex-c


People also ask

Which operator is used to check the object has the specified type in Dart?

To achieve the same result in Dart, we can use the type test operator is . It returns true if the object has the specified type.

What is runtimeType in flutter?

The runtimeType property helps to find what kind of data is stored in the variable using var keyword. In the next section, we'll explore usage of this property in for different type of data.

How do you know what data type you are in darts?

To check the type of a variable in Flutter and Dart, you can use the runtimeType property.


1 Answers

Use is.

You can do c is a. Notice that new a() is a is true as well. If you really want to know if an instance is a subtype of another type, you might ask c is a && c.runtimeType != a.

like image 182
Harry Terkelsen Avatar answered Oct 24 '22 02:10

Harry Terkelsen