All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart2-v3template.enum.mustache Maven / Gradle / Ivy

There is a newer version: 8.1
Show newest version

enum {{classname}} {
  {{#allowableValues}}
    {{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}
  {{/allowableValues}}
}

extension {{classname}}Extension on {{classname}} {
  String get name => toMap[this];

  // you have to call this extension class to use this as this is not yet supported
  static {{classname}} type(String name) => fromMap[name];

  static Map fromMap = {  {{#allowableValues}}
  {{#enumVars}}{{{value}}}:{{classname}}.{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}
{{/allowableValues}} };
  static Map<{{classname}}, String> toMap = {  {{#allowableValues}}
  {{#enumVars}}{{classname}}.{{{name}}}:{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}
{{/allowableValues}} };

  static {{classname}} fromJson(dynamic data) => data == null ? null : fromMap[data];

  dynamic toJson() => toMap[this];

  static List<{{classname}}> listFromJson(List json) =>
    json == null ? <{{classname}}>[] : json.map((value) => fromJson(value)).toList();

  static {{classname}} copyWith({{classname}} instance) => instance;

  static Map mapFromJson(Map json) {
    final map = {};
    if (json != null && json.isNotEmpty) {
      json.forEach((String key, dynamic value) => map[key] = fromJson(value));
    }
    return map;
  }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy