What will be the data we get in runtimeType → function name or else class name or anything else ?
Answer
Overview
In Dart, is a property available on every object. It returns the actual runtime type of the object as a textruntimeType
text
TypeWhat Does runtimeType Return?
dartvoid 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> }
✅
always returns the class name of the object at runtime.textruntimeType
With Custom Classes
dartclass 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:
returns the actual instantiated class, not the declared variable type.textruntimeType
With Functions
dartvoid 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,
returns the function signature, not the function name.textruntimeType
With Widgets in Flutter
dartWidget buildWidget() => Text('Hello'); void main() { final widget = buildWidget(); print(widget.runtimeType); // Text }
runtimeType vs is Operator
dartclass 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 | text |
|---|---|---|
| Exact class match | ✅ Yes | ✅ Yes |
| Parent class match | ❌ No | ✅ Yes |
| Recommended for type check | ❌ Avoid | ✅ Use this |
Practical Use Cases
Debugging
dartList<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 textis
instead for production)
text
isdartvoid 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
| Scenario | What runtimeType Returns |
|---|---|
text | text |
text | text |
text text | text |
text | Function signature e.g. text |
text | text |
Best Practice: Use
only for debugging/logging. For type checking in code, always use thetextruntimeTypeoperator — it is safer and handles inheritance correctly.textis