Skip to content
pubscout

reflectable

5.2.3, released 6 months ago by an unverified publisher

Provides runtime reflection via code generation with configurable capabilities.

WASM-ready
$ flutter pub add reflectable
Source repository

What it does

The package generates code that enables reflective operations such as invoking methods, accessing fields, and inspecting types, limited to the capabilities you declare. It works together with a builder that runs during build time.

AI-written summary of version 5.2.3, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need reflection but want to avoid the heavy runtime cost of dart:mirrors.
  • You want fine‑grained control over which reflective features are available.
  • You are building a CLI or server app where code generation is acceptable.

Skip it when

  • You target Flutter web or mobile where code generation adds build complexity and size.
  • You need dynamic reflection that cannot be statically constrained.
  • You prefer a zero‑config, runtime‑only solution.

Strengths

  • Fine‑grained capability control reduces runtime footprint.
  • Supports all Dart platforms (android, ios, web, windows, macos, linux).
  • No need for dart:mirrors which is not available on all platforms.

Weaknesses

  • Requires build_runner and a separate builder package.
  • Generated code must be imported and initialized manually.
  • Not suitable for packages that will be published with generated files.

Setup

Kind
Pure Dart
Effort
Medium
Code generation
Yes (build_runner)
Direct dependencies
0

Add reflectable to dependencies and reflectable_builder to dev_dependencies. Create build.yaml to configure the generator. Run `dart run build_runner build` and import the generated .reflectable.dart file. Call initializeReflectable() before using reflection.

```dart
import 'package:reflectable/reflectable.dart';
import 'main.reflectable.dart';

class Reflector extends Reflectable {
  const Reflector() : super(invokingCapability);
}

const reflector = const Reflector();

@reflector
class A {
  final int a;
  A(this.a);
  bool greater(int x) => x > a;
}

void main() {
  initializeReflectable();
  var x = A(10);
  var m = reflector.reflect(x);
  print(m.invoke('greater', [3]));
}
```

Alternatives

Packages that do the same job and could replace reflectable.

Works with

Packages commonly used together with reflectable: ones it builds on, ones built on it, and its own family.