pub package Null Safety Codecov CI GitHub Tag New Commits Last Commits Pull Requests Code size License

reflection_factory allows Dart reflection with an easy approach, even for third-party classes, using code generation portable for all Dart platforms.


To enable/generate reflection for some class/type, you can use two approaches:

  • @EnableReflection():

    Annotation that indicates that a specific class/type will have reflection.

  • @ReflectionBridge([User, Profile]):

    Annotation that indicates through a bridge class that the types (User and Profile) will have reflection.


The annotations @EnableReflection is used above your class/type that you want to have reflection enabled.

File: some_source_file.dart:

import 'package:reflection_factory/reflection_factory.dart';

// Add a reference to the code generated by:
// $> dart run build_runner build
part 'some_source_file.reflection.g.dart';

// Indicates that reflection for class `User` will be generated/enabled:
class User {
  String? email;

  String pass;

  User(, this.pass);

  User.empty() : this(null,'') ;

  bool get hasEmail => email != null;

  bool checkPassword(String pass) {
    return this.pass == pass;

void main() {
  var user = User('', '123');

  // The generated reflection:
  var userReflection = user.reflection;

  var fieldEmail = userReflection.field('email')!;
  print('email: ${fieldEmail.get()}');

  var methodCheckPassword = userReflection.method('checkPassword')!;

  var passOk1 = methodCheckPassword.invoke(['wrong']); // false
  print('pass("wrong"): $passOk1');

  var passOk2 = methodCheckPassword.invoke(['123']); // true
  print('pass("123"): $passOk2');

  // Using the generated `toJson` extension method:
  print('User JSON:');

  // Using the generated `toJsonEncoded` extension method:
  print('User JSON encoded:');
  // Accessing reflection through class:
  var userReflection2 = User$reflection();

  // Creating an `User` instance from default or empty constructor:
  var user2 = userReflection2.createInstance()!; = '';
  user2.pass = 'abc';

  print('User 2 JSON:');


pass("wrong"): false
pass("123"): true
User JSON:
{email:, hasEmail: true, pass: 123}
User JSON encoded:
User 2 JSON:
{email:, hasEmail: true, pass: abc}


The annotations @ReflectionBridge is used above a bridge class, and indicates that third-party types will have reflection generated.

File: some_source_file.dart:

import 'package:reflection_factory/reflection_factory.dart';

// Class `User` is from a third-party package:
import 'package:some_api/user.dart';

// Add a reference to the code generated by:
// $> dart run build_runner build
part 'some_source_file.reflection.g.dart';

// Indicates that reflection for class `User` will be generated/enabled
// through a bridge class:
class UserReflectionBridge {}

void main() {
  var user = User('', '123');

  // The generated reflection through bridge class:
  var userReflection = UserReflectionBridge().reflection(user);

  var fieldEmail = userReflection.field('email')!;
  print('email: ${fieldEmail.get()}');

  print('User JSON encoded:');


User JSON encoded:


You need to add 2 dependencies in your project:

File: pubspec.yaml

  reflection_factory: ^1.0.0

  build_runner: ^2.1.1

Building/Generating Code

To generate the reflection code just run build_runner in your Dart project:

$> dart run build_runner build


The official source code is hosted @ GitHub:

Features and bugs

Please file feature requests and bugs at the issue tracker.


Any help from the open-source community is always welcome and needed:

  • Found an issue?
    • Please fill a bug report with details.
  • Wish a feature?
    • Open a feature request with use cases.
  • Are you using and liking the project?
    • Promote the project: create an article, do a post or make a donation.
  • Are you a developer?
    • Fix a bug and send a pull request.
    • Implement a new feature.
    • Improve the Unit Tests.
  • Have you already helped in any way?
    • Many thanks from me, the contributors and everybody that uses this project!

If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.


Graciliano M. Passos: gmpassos@GitHub.


Apache License - Version 2.0


Library for reflection factory.
Library for reflection_factory's builder.