This model generator can be used to generate JsonSerializable models
flutter packages run model_generator
Example of the pubspec.yaml
file if you want to use a custom base_directory for all your models
Default is /lib/model
you can override it for all your models like this:
model_generator:
base_directory: custom_models
this will write all your models to /lib/custom_models
path
will be added after the base_directory
##FVM support
If you are using fvm for managing your flutter version. You can add an option to the model generator as well to run with fvm.
add an option use_fvm
and set it to true. (by default it is set to false)
model_generator:
use_fvm: true
Example of the model_generator/config.yaml
file
UserModel:
path: webservice/user
properties:
id:
type: int
name:
type: string
salary:
type: double
something:
type: dynamic
isLoggedIn:
type: bool
roles:
type: array
items:
type: string
birthday:
type: date
addresses:
type: array
items:
type: Address
securityRole:
type: string
jsonKey: securityIndicator
dynamicField:
type: dynamic
includeIfNullField:
includeIfNull: false #If this field is null, this field will not be added to your json object (used for PATCH models)
type: string
ignoreField:
ignore: false #this field will not be final, and not be used in the json parsing
type: string
mutableField:
non_final: true #Field will not be marked final
type: string
Address:
path: webservice/user
properties:
street:
type: string
#Custom base_directory
CustomBaseDirectoryObject:
base_directory: custom_models
path: webservice
properties:
path:
type: string
Add enums with custom values
Gender:
path: webservice/user
type: enum
properties:
MALE:
value: _mAl3
FEMALE:
value: femAle
X:
value: X
Y:
Support for custom objects that are not generated by the model generator
CustomObject:
path: data/custom/
type: custom
factory {Model_Name}.fromJson(Map<String, dynamic> json) => _${Model_Name}FromJson(json);
Map<String, dynamic> toJson() => _${Model_Name}ToJson(this);
Support for custom objects but use fromJson & toJson instead of full object parsing:
CustomObjectFromToJson:
path: data/custom/
type: custom_from_to_json
{Model_Name} handle{Model_Name}FromJson(object) => {Model_Name}.fromJson(object);
{Original_Type} handle{Model_Name}ToJson({Model_Name} data) => data.toJson();