empty_generator 0.0.3
empty_generator: ^0.0.3 copied to clipboard
Code generator for use with the Empty Annotation package.
This package provides a builder for the Dart Build System that generates Empty constructors for classes annotated with empty_annotation.
This library allows you to generate empty constructors for classes, like so:
myInstance = MyClassEmpty(); // Generate an empty instance of MyClass.
Usage #
In your pubspec.yaml file
- Add to dependencies section empty_annotation: ^1.0.0
- Add to dev_dependencies section empty_generator: ^1.0.0
- Add to dev_dependencies section build_runner: ^2.1.7
- Set environment to at least Dart 3.0.0 version like so: ">=3.0.0 <4.0.0"
Your pubspec.yaml should look like so:
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
...
empty_annotation: ^1.0.0
dev_dependencies:
...
build_runner: ^2.4.13
empty_generator: ^1.0.0
Annotate your class with Empty annotation
import 'package:empty_annotation/empty_annotation.dart';
part 'your_class.g.dart';
@Empty()
class YourClass {
final String name;
final int age;
YourClass({required this.name, required this.age});
}
Make sure that you set the part file as shown in the example above: part 'your_file_name.g.dart';.
Launch code generation
flutter pub run build_runner build
Use
final emptyInstance = YourClassEmpty();
print('Generated empty instance: $emptyInstance');
Additional features #
Generate Empty Constructor
The package generates an empty constructor for your class:
final emptyInstance = YourClassEmpty(); // Creates an empty instance
Use @Default() to override default values
@Empty()
class MyClass {
@Default('John')
final String name;
@Default(18)
final int age;
@Default(MyEnum.optionA)
final MyEnum type;
MyClass({required this.name, required this.age, required this.type});
}
Note: Enums, primitive types, and even constant values can be set with @Default() and will be respected by the generator.
build.yaml configuration
You can globally configure the library's behavior in your project by adding a build.yaml file.
targets:
$default:
builders:
empty_generator:
enabled: true