named_fields_generator 0.2.0
named_fields_generator: ^0.2.0 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
*Fieldshelper class for each annotated class. - Static
Stringconstants for each non-static field. - A
List<String>containing all field names. - A map of property getters for easy field access at runtime.
Getting started #
Add the dependency to your pubspec.yaml:
dependencies: named_fields_generator: ^0.1.0
Also add build_runner as a dev dependency (if not already):
dev_dependencies: build_runner: ^2.3.3
Usage #
Source Code Setup
To use the generator, you need two things:
- Import the annotation library and annotate the class with
@NamedFields. - Add a
partdirective that points to the generated file. The generated file name will always be the same as the source file with.g.dartappended.
Example:
import 'package:named_fields_generator/annotations/annotations.dart';
part 'person.g.dart';
@NamedFields()
class Person {
final String name;
final int age;
Person(this.name, this.age);
}
Running the Code Generator
Run the following command to generate the .g.dart file once:
dart run build_runner build
Or keep a watcher running to automatically regenerate files when source code changes:
dart run build_runner watch
For more details, see the build_runner documentation
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.