Introduction
A Dart CLI tool for analyzer-based, extremely fast, and clean data class code generation.
Parses Dart code into an AST using only the Dart analyzer, building classes completely independently of build_runner, achieving unparalleled build speed (π Takes 5 ~ 10ms per file on average).
Support
π’ Supported π‘ Planned π΄ Not Supported
| Future | Status | Usage |
|---|---|---|
| π Immutability | π’ | It's still required |
| ποΈ Private fields | π’ | Support private fields with getters. |
| π copyWith (Clone) | π’ | @Datagen(copyWith: true) |
| π¦ JSON serialization | π’ | @Datagen(fromJson: true, fromJsonList: true, toJson: true) |
| π Stringify | π’ | @Datagen(stringify: true) |
| βοΈ Equality | π’ | @Datagen(equality: true) |
Preview
Development
When developing, thereβs no need to follow any special format like freezed or mappable. As long as you write proper const constructors, the Datagen library builder will automatically adjust the code to fit standard Dart conventions. Also, itβs highly recommended to use the
watchfeature during development!
Usage
Learn how to quickly set up and use this library for generating data classes.
Annotate Your Class
The following is an example of using the @datagen or @Datagen() annotation on a class.
Applying this annotation enables Datagen to generate utility features such as
copyWith, JSON serialization, stringify, and equality overrides based on the configuration options.
@Datagen(omitFactory: true)
class A {
const A({
required String name,
int age = 20,
DateTime? dateTime,
required dynamic status,
// Specifies the target type to convert the field to using `@Get`.
// Only needs to indicate the target type and the actual type in the data class.
@Get(int) required String minute,
});
// Use `dynamic` and a custom getter for fields not supported by `@Get`,
// e.g., the `status` field may require custom conversion.
@override
Enum get status {...}
}
Build the Data Class
Generate the code using:
dart run datagen build
Using Watch Mode
Automatically rebuild on file changes:
dart run datagen watch
Tip
Build Integration: To integrate and manage build processes with tools like resourcegen, learn how to use Prepare effectively.
Options
Arguments for the @Datagen() annotation.
| Name | Description | Default |
|---|---|---|
| copyWith | Enables generating a copyWith method for the annotated class. | true |
| fromJson | Enables generating a fromJson factory constructor. | true |
| toJson | Enables generating a toJson method. | true |
| stringify | Enables generating a toString override method. | true |
| equality | Enables generating a hashCode and operator == override. | true |
| omitFactory | Controls whether a fromJson factory is generated in the public class. The factory in the generated .datagen.dart class is always created. | false |
How to Set Config File?
Create a file named datagen.json in the folder where you run the CMD command, and write the following configuration inside:
{
"options": {
"copyWith": true,
"fromJson": true,
"fromJsonList": true,
"toJson": true,
"stringify": true,
"omitFactory": false
},
"useCommand": true
}
IDE Settings Guide
This table provides quick access to IDE-specific tips and guides.
| IDE | Docs |
|---|---|
| Visual Studio Code | VSCODE_GUIDE.md |