Reflection and introspection

suggest change

Introspection allows a program to examine the type and properties of objects at runtime, while reflection allows examining and invoking classes, methods, and fields dynamically.

In Dart, there is a fundamental distinction between runtime type introspection (which is always supported) and full dynamic reflection (which is restricted in compiled AOT environments like Flutter).

Basic Type Introspection

All Dart objects inherit from Object and provide built-in type inspection capabilities:

1. runtimeType Property

Every object exposes a runtimeType property representing its concrete type:

void main() {
  dynamic value = 'Hello Dart';
  print(value.runtimeType); // String

  value = [1, 2, 3];
  print(value.runtimeType); // List<int>
}

2. Type Testing with is and is!

Use the is operator to test an object's type, which automatically enables type promotion:

void inspectObject(Object obj) {
  if (obj is String) {
    // obj is automatically promoted to String
    print('String of length: ${obj.length}');
  } else if (obj is List<int>) {
    print('Integer list with sum: ${obj.fold(0, (a, b) => a + b)}');
  } else {
    print('Unknown object type: ${obj.runtimeType}');
  }
}

Runtime Reflection with dart:mirrors (Dart VM Only)

The Dart SDK includes dart:mirrors for full runtime reflection, but it is only supported in the standalone JIT Dart VM (command-line/server-side Dart). It is explicitly disabled in Flutter and Dart Web to allow ahead-of-time (AOT) tree-shaking and dead-code elimination.

import 'dart:mirrors';

class Person {
  final String name;
  int age;

  Person(this.name, this.age);

  void celebrateBirthday() {
    age++;
    print('Happy birthday $name! You are now $age.');
  }
}

void main() {
  var person = Person('Alice', 29);

  // Reflect on an instance
  InstanceMirror instanceMirror = reflect(person);
  ClassMirror classMirror = instanceMirror.type;

  print('Class name: ${MirrorSystem.getName(classMirror.simpleName)}');

  // Inspect declarations
  classMirror.declarations.forEach((symbol, declaration) {
    print('Member: ${MirrorSystem.getName(symbol)}');
  });

  // Dynamically invoke a method
  instanceMirror.invoke(#celebrateBirthday, []);
}

The Modern Alternative: Compile-Time Code Generation

Because dart:mirrors is unavailable in Flutter and web apps, the Dart ecosystem uses compile-time introspection and code generation instead of runtime reflection.

Popular packages like json_serializable, freezed, and injectable inspect Dart source code at compile time via the analyzer (source_gen) and generate strongly typed serialization and dependency injection code:

# Typical code generation workflow
dart run build_runner build --delete-conflicting-outputs

Inspecting Stack Traces

To inspect the execution callstack programmatically:

void logCallsite() {
  final trace = StackTrace.current;
  print('Current stack trace:\n$trace');
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents