Dart – Get Type of Object

To get type of an object in runtime, programmatically, in Dart, we can use the runtimeType property of the Object class.

Syntax

The syntax to get the runtime type of the object obj is

</>
Copy
var obj = value;
var type = obj.runtimeType;

Examples

Get Type for Inbuilt Datatype

In the following example, we take an integer value in an object, and find the type of this object using Object.runtimeType property.

main.dart

</>
Copy
void main() {
  var obj = 25;
  var type = obj.runtimeType;
  print(type);
}

Output

int

Get Type for Custom Datatype

In the following example, we define a type named Person, create an object of this type, and find the type of this object using Object.runtimeType property.

main.dart

</>
Copy
class Person {
  String? name;
  Person(String name) {
    this.name = name;
  }
}

void main() {
  var obj = new Person('Abc');
  var type = obj.runtimeType;
  print(type);
}

Output

Person

Conclusion

In this Dart Tutorial, we learned how to get the type of an object in runtime programmatically, using Object.runtimeType property, with examples.