named_fields_generator 0.0.1
named_fields_generator: ^0.0.1 copied to clipboard
A Dart code generator for @NamedFields annotations.
named_fields_generator #
A Dart code generator for the @NamedFields annotation. It generates static field name constants, a list of all field names, and property getter maps for annotated classes.
Features #
- Generates a *Fields class for each class annotated with @NamedFields.
- Static String constants for each non-static field.
- A list of all field names.
- A map of property getters for easy field access.
Getting started #
Add the dependency to your pubspec.yaml:
yaml dependencies: named_fields_generator: ^0.1.0
Usage #
Annotate your class with @NamedFields:
`import 'package:named_fields_generator/annotations/annotations.dart';
@NamedFields() class Person { final String name; final int age; static const String type = 'person'; Person(this.name, this.age); }`
Run the generator:
dart run build_runner build
This generates a PersonFields class:
`// Generated field names for Person class PersonFields { static const String name = 'name'; static const String age = 'age';
static const List
static final Map<String, Object? Function(Person)> getters = { 'name': (obj) => obj.name, 'age': (obj) => obj.age, }; }`
Additional information #
Only non-static fields are included. For issues and contributions, please open an issue or pull request on GitHub. Requires Dart SDK >= 3.9.2.