Dart Sealed Class Generator
Generate sealed class hierarchy for Dart and Flutter.
Features
- Generate sealed class with abstract super type and data sub-classes.
- Static factory methods. for example
Result.success(data: 0)
. - Cast methods. for example
a.asSuccess
,a.isSuccess
ora.asSuccessOrNull
. - Three types of equality and hashCode generation : data (like kotlin data classes), identity and distinct.
- Implement data equality with popular equatable library.
- Support for generics. even types can be mixed.
- Support for nullable and non-nullable types in null-safe projects.
- Support for using one sealed type in another.
- Support for null-safety.
- Generate toString for data classes.
- Generate 6 types of different matching methods. like
when
,maybeWhen
andmap
.
Usage
Add dependencies in your pubspec.yaml
file.
Import sealed_annotations
.
Add part
pointing to a file which you want classes be generated in. with .sealed.dart
extension.
Add @Sealed
annotation, and an abstract private class as a manifest for generated code. For example:
Then run the following command to generate code for you. If you are developer for flutter:
And if you are developing for pure dart:
The generated code will look like: (the following code is summarised)
Notes:
- Prefer using factories in super class instead of sub-class constructors. like
Whether.rainy()
instead
ofWhetherRainy()
- Minimize usage of cast methods, most of the time they can be replaced with a match method.
Equality and generated class names
You can choose between three types of equality using @WithEquality(...)
annotation. Default equality is data
if not
specified. This will become default equality for all sub-classes. You can change equality of each sub-class by using
this annotation on individual methods.
Equality types:
data
Equality is implemented with Equatable package. It behaves like kotlin data classes.identity
Only identical instances are equal. It's like when you don't implement any specific equality.distinct
All the instances are not equal with each other. Even an instance is not equal with itself.
A basic example:
In the proceeding example all classes will have data
equality. For example if you wanted identity
equality for all
classes but using distinct
equality for windy
:
An abstract super class is generated with name equal to name of manifest class without the underline (here Weather
).
Each method will become a sub-class. There should be at least one method. sub-class names are based on method name
prefixed with super class name (for example WeatherSunny
). Naming process can be tailored with use of @WithPrefix
and @WithName
annotations. Each method argument will become a field in corresponding sub-class. Field names are equal
to argument names and field types are equal to argument types or dynamic if not specified. Argument types can be
overridden using @WithType
annotation for example when type information is not available at build time. Note that you
can have nullable and non-nullable fields.
To change prefix of sub-class names which by default is top class name, you can use @WithPrefix
annotation. for
example:
Now sunny
will be named HelloSunny
instead of the default WeatherSunny
. You can use @WithPrefix('')
to remove
all prefix from sub-class names.
To change sub-class names directly you can use @WithName
annotation. It will override WithPrefix
if specified. for
example:
Now sunny
will be named Hello
instead of the default WeatherSunny
. This is useful if you want not to use prefix
for some items.
Almost all methods on sealed classes use short names extracted from manifest method names. Full sub-class names are not
used. It is recommended not to use sub-classes directly. There are factory methods for each item on super class.
Generic Usage
For generic sealed classes you should write manifest class like a generic class which you are implementing.
It is recommended that if you want nullable generic fields, declare a generic parameter as T extends Base?
and use T
without nullability suffix. If you want non-nullable generic fields declare a generic parameter as T extends Base
and
use T
without nullability suffix. If you don't specify upper bound it will default to Object?
so your generic types
will be nullable.
Or you can have multiple generic types and even mix them.
Dynamic types and Using one sealed type in another
Consider you have a sealed result type like:
You want to use this type in another sealed type.
If you generate for WeatherInfo
you will see that result has dynamic
type. It is because Result
itself is not code
generated at build time.
You should use @WithType
annotation.
Common Fields
Sometimes you need some fields to be present in all of your sealed classes. For example consider making a sealed class
for different types of errors, and all of them are required to have code
and message
. It is very annoying to add
code and message to all of sealeds manually. Also if you have an error object you are unable to get its code or message
without using cast or match methods. Here you can use common fields.
To declare a common field you can add a getter or a final field to a manifest class, and it will automatically be added
to all of your sealed classes. for example:
common fields are available on ApiError
objects as well as it's sub-classes.
If you specify common fields in your seaeld classes it has no effect. for example:
You can use sub-class of common field type in sealed classes. For example:
common fields also works with other constructs of dart_sealed like generics and @WithType. for example:
and, for example:
Ignoring Generated Files
It is recommended to ignore generated files on Git. Add this to your .gitignore
file:
*.sealed.dart
It is NOT recommended to exclude generated files from analysis. But if you decide to do so, add this to
your analysis_options.yaml
file:
analyzer:
exclude:
- **.sealed.dart