model_generator 4.3.0
model_generator: ^4.3.0 copied to clipboard

Dart tool to automaticly generate models from a yml file to speed up your development flow.

Model Generator #

This model generator can be used to generate JsonSerializable models

pub package Build Status Coverage Status MIT license

Run #

flutter packages run model_generator

Model file #

By default, the model generator looks for the model yaml file in model_generator/config.yaml. If you want to overwrite this, specify it in your pubspec.yaml file by using config_path. Example of the pubspec.yaml file if you want to use a custom model file location:

model_generator:
  config_path: my_model_dir/config.yaml

You can also specify a command line parameter to override the location for a single run. For this, use the --path command line parameter. Example:

flutter packages run model_generator --path my_other_model_dir/config.yaml

Default setup #

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

##== and hashCode If you want the generated models to include code for == and hashCode, you can turn it on in pubspec.yaml. Defaults to false. All fields are taken into consideration for the generated code.

model_generator:
  equals_and_hash_code: true

or to override the values per object:

UserModel:
  path: webservice/user
  equals_and_hash_code: false
  properties:
    id:
      type: int

##toString If you want the generated models to include generated toString code, you can turn it on in pubspec.yaml. Defaults to false. All fields are taken into consideration for the generated code.

model_generator:
  to_string: true

or to override the values per object:

UserModel:
  path: webservice/user
  to_string: false
  properties:
    id:
      type: int

##Extra imports and annotations If you wish for extra import statements in the generated files and/or extra annotations on the generated model classes, you can specify those in pubspec.yaml

model_generator:
  extra_imports:
    - 'package:flutter/foundation.dart'
  extra_annotations:
    - '@immutable'

or to override the values per object:

UserModel:
  path: webservice/user
  extra_imports:
  extra_annotations:
    - '@someAnnotation'
  properties:
    id:
      type: int

##Generics support support If you want your models to generate code that can be used in combination with generics. use this:

model_generator:
  generate_for_generics: true

or to override the default generate_for_generics value in the pubspec.yaml

UserModel:
  path: webservice/user
  generate_for_generics: true
  converters:
    - DateTimeConverter
  properties:
    id:
      type: int

Default setup #

Example of the model_generator/config.yaml file

UserModel:
  path: webservice/user
  converters:
    - DateTimeConverter
  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
    idToAddress:
      type: map
      items:
        key: String
        value: Address
    securityRole:
      type: string
      jsonKey: securityIndicator
    dynamicField:
      type: dynamic
    includeIfNullField:
      include_if_null: 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
    changedAt:
      type: datetime

Address:
  path: webservice/user #Can also be package:... and/or end with the actual file (.dart)
  properties:
    street:
      type: string

#Custom base_directory
CustomBaseDirectoryObject:
  base_directory: custom_models
  path: webservice
  properties:
    path:
      type: string

#Custom json converter. Use with converters property on models
DateTimeConverter:
  type: json_converter
  path: converter/

Enum support #

Add enums with custom values

Gender:
  path: webservice/user
  type: enum
  properties:
    MALE:
      value: _mAl3
    FEMALE:
      value: femAle
    X:
      value: X
    Y:

Use unknownEnumValue #

UnknownEnumTestObject:
  path: webservice
  properties:
    path:
      unknown_enum_value: X
      type: Gender

Custom object #

Support for custom objects that are not generated by the model generator

CustomObject:
  path: data/custom/
  type: custom

Required methods inside your class #

  factory {Model_Name}.fromJson(Map<String, dynamic> json) => _${Model_Name}FromJson(json);

  Map<String, dynamic> toJson() => _${Model_Name}ToJson(this);

fromJson & toJson #

Support for custom objects but use fromJson & toJson instead of full object parsing:

CustomObjectFromToJson:
  path: data/custom/
  type: custom_from_to_json

Required functions outside your class #

{Model_Name} handle{Model_Name}FromJson(object) => {Model_Name}.fromJson(object);

{Original_Type} handle{Model_Name}ToJson({Model_Name} data) => data.toJson();

JsonConverter support #

You can specify custom json converters to be used for types that match

UserModel:
  path: webservice/user
  converters:
    - DateTimeConverter
  properties:
    changedAt:
      type: datetime

Specify the custom JsonConverter object as a known type to resolve it

DateTimeConverter:
  type: json_converter
  path: converter/
4
likes
110
pub points
46%
popularity

Publisher

icapps.com

Dart tool to automaticly generate models from a yml file to speed up your development flow.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

args, meta, path, yaml

More

Packages that depend on model_generator