package:source_gento eliminate the time you spend writing boilerplate serialization code for your models.
pubspec.yaml, you need to install the following dependencies:
package:build_runner, you can build models automatically, anywhere in your project structure, by running
pub run build_runner build.
buildcall with a call to
watch. They take the same parameters.
@serializableannotation to your model class to have it serialized, and a serializable model class's name should also start with a leading underscore.
abstractclass to ensure immutability of models.
angel3_serializedoes it for you. This means that the main class can have its constructors automatically generated, in addition into serialization functions.
Bookmodel. Create a class named
Book: Extends or implements
_Book; may be
BookSerializer: static functionality for serializing
BookFields: The names of all fields from the
BookDecoder: Also allows
bookSerializer: A top-level,
Book.toString: Prints out all of a
2.0.2, the generated output also includes information about the serialized names of keys on your model class.
angel3_serializepulls in fields from parent classes, as well as implemented interfaces, so it is extremely easy to share attributes among model classes:
angel3_serializewill transform keys into snake case. Use
aliasto provide a custom name, or pass
falseto the builder;
@requiredin the generated constructor, and serializers will check for its presence, throwing a
FormatExceptionif it is missing.
package:angel3_serializedoes not cover every known Dart data type; you can add support for your own. Provide
@SerializableField()as you see fit.
deserializerwill always be
serializercan receive the data type in question.
serializesToargument. This lets the generator, as well as the ORM, apply the correct (de)serialization rules and validations.
angel3_serializealso supports a few types of nesting of
_Book), or the generated class name (ex
Author) depends on a model defined in another file (
Book), then you will need to generate
author.g.dart, in a separate build action. This way, the analyzer can resolve the
updatedAtfields for you, in the style of an Angel
Model. This will automatically be generated, only for classes extending
Uint8Listfields, by means of serialization to and from
_Authorclass will generate the following in
canSerialize: falsewill not be present in the TypeScript definition. The rationale for this is that if a field (i.e.
password) will never be sent to the client, the client shouldn't even know the field exists.
angel3_serializecan forward custom constructor parameters.