Question #148MediumOOP Concepts

What will be the data we get in runtimeType → function name or else class name or anything else ?

Answer

Overview

In Dart,

text
runtimeType
is a property available on every object. It returns the actual runtime type of the object as a
text
Type
object — which is the class name, not the function name or anything else.


What Does runtimeType Return?

dart
void main() {
  int a = 42;
  double b = 3.14;
  String c = 'hello';
  bool d = true;
  List<int> e = [1, 2, 3];

  print(a.runtimeType); // int
  print(b.runtimeType); // double
  print(c.runtimeType); // String
  print(d.runtimeType); // bool
  print(e.runtimeType); // List<int>
}

text
runtimeType
always returns the class name of the object at runtime.


With Custom Classes

dart
class Animal {
  void speak() => print('...');
}

class Dog extends Animal {
  
  void speak() => print('Woof!');
}

void main() {
  Animal a = Animal();
  Animal d = Dog(); // Declared as Animal, but actually a Dog

  print(a.runtimeType); // Animal
  print(d.runtimeType); // Dog ← returns actual runtime class, not declared type!
}

Key Insight:

text
runtimeType
returns the actual instantiated class, not the declared variable type.


With Functions

dart
void greet() => print('Hello');
int add(int a, int b) => a + b;

void main() {
  print(greet.runtimeType); // () => void
  print(add.runtimeType);   // (int, int) => int
}

For functions,

text
runtimeType
returns the function signature, not the function name.


With Widgets in Flutter

dart
Widget buildWidget() => Text('Hello');

void main() {
  final widget = buildWidget();
  print(widget.runtimeType); // Text
}

runtimeType vs is Operator

dart
class Vehicle {}
class Car extends Vehicle {}

void main() {
  final car = Car();

  // runtimeType — checks exact type
  print(car.runtimeType == Car);      // true
  print(car.runtimeType == Vehicle);  // false (not exact type)

  // is — checks type hierarchy (preferred for checks)
  print(car is Car);      // true
  print(car is Vehicle);  // true  ← includes parent classes
}
Check
text
runtimeType ==
text
is
operator
Exact class match✅ Yes✅ Yes
Parent class match❌ No✅ Yes
Recommended for type check❌ Avoid✅ Use this

Practical Use Cases

Debugging

dart
List<dynamic> items = [42, 'hello', 3.14, true];

for (final item in items) {
  print('Value: $item | Type: ${item.runtimeType}');
}
// Value: 42 | Type: int
// Value: hello | Type: String
// Value: 3.14 | Type: double
// Value: true | Type: bool

Type Switching (use
text
is
instead for production)

dart
void describe(dynamic value) {
  if (value is int) {
    print('An integer: $value');
  } else if (value is String) {
    print('A string: "$value"');
  } else if (value is List) {
    print('A list with ${value.length} items');
  }
}

Summary

ScenarioWhat runtimeType Returns
text
42.runtimeType
text
int
text
'hello'.runtimeType
text
String
text
Dog().runtimeType
(where
text
Dog extends Animal
)
text
Dog
text
greet.runtimeType
(a function)
Function signature e.g.
text
() => void
text
Text('Hi').runtimeType
(Flutter widget)
text
Text

Best Practice: Use

text
runtimeType
only for debugging/logging. For type checking in code, always use the
text
is
operator — it is safer and handles inheritance correctly.