createDart static method

List<String> createDart(
  1. String className,
  2. Map map
)

Implementation

static List<String> createDart(String className, Map map) {
  List<String> list = [];
  List<List<String>> inList = [];

  list.add("class $className {\n");

  String allocStr = "\n$className ({";

  //生成属性
  for (String key in map.keys) {
    var value = map[key];

    if (value != null) {
      allocStr = "$allocStr this.${key},";
      if (value is Map) {
        list.add("${firstBig(key)}? ${key};");
      } else if (value is List) {
        if (value.length > 0) {
          var firstObj = value.first;
          if (firstObj is Map) {
            list.add("List<${firstBig(key)}>? ${key};");
          } else if (firstObj is List) {
            list.add("List<List>? ${key};");
          } else if (firstObj is bool) {
            list.add("List<bool>? ${key};");
          } else {
            list.add("List<String>? ${key};");
          }
        }
      } else if (value is bool) {
        list.add("bool? ${key};");
      } else {
        list.add("String? ${key};");
      }
    }
  }

  list.add("$allocStr});\n");

  //生成fromJson方法
  list.add("\n${className}.fromJson(Map<String, dynamic> json) {\n");
  for (String key in map.keys) {
    var value = map[key];
    if (value != null) {
      if (value is Map) {
        //是map
        //list.add("\tif (json.containsKey('${key}')) {${key} = json['${key}'] != null ? new ${firstBig(key)}.fromJson(json['${key}']): null;} else {${key} = null;}");

        String tmp = """
  if (json.containsKey('${key}')) {
    var tmp = json['${key}'];
    if (tmp is Map) {
      ${key} = json['${key}'] != null ? new ${firstBig(key)}.fromJson(json['${key}']) : null;
    } else {
      ${key} = null;
    }
  } else {
    ${key} = null;
  }
        """;

        list.add(tmp);

        var mapList = createDart("\n\n${firstBig(key)}", value);
        inList.add(mapList);
      } else if (value is List) {
        //是List

        if (value.length > 0) {
          var firstObj = value.first;
          if (firstObj is Map) {
            //list.add("if (json.containsKey('${key}')) {if (json['${key}'] != null) {${key} = <${firstBig(key)}>[];json['${key}'].forEach((v) {${key}!.add(new ${firstBig(key)}.fromJson(v));});} else {${key} = [];}} else {${key} = [];}");

            String tmp = """
  if (json.containsKey('${key}')) {
    if (json['${key}'] != null && json['${key}'] is List) {
      ${key} = <${firstBig(key)}>[];
      json['${key}'].forEach((v) {
        ${key}!.add(new ${firstBig(key)}.fromJson(v));
      });
    } else {
      ${key} = [];
    }
  } else {
    ${key} = [];
  }
            """;

            list.add(tmp);

            var mapList = createDart("\n\n${firstBig(key)}", firstObj);
            inList.add(mapList);
          } else if (firstObj is List) {
            print("\t***数组里面套用数组解析出错******************\n");
            print("\t***数组里面套用数组解析出错********************\n");
            print("\t***数组里面套用数组解析出错**********************\n");
            print("\t***数组里面套用数组解析出错************************\n");
            assert(false, "数组里面套用数组解析出错");
          } else if (firstObj is bool) {
            //list.add("\tif (json.containsKey('${key}')) {${key} = json['${key}'].cast<bool>();} else {${key} = [];}");
            String tmp = """
  if (json.containsKey('${key}')) {
    var tmp = json['${key}'];
    if (tmp is List) {
      ${key} = json['${key}'].cast<bool>();
    } else {
      ${key} = [];
    }
  } else {
    ${key} = [];
  }
            """;

            list.add(tmp);
          } else {
            //list.add("\tif (json.containsKey('${key}')) {${key} = json['${key}'].cast<String>();} else {${key} = [];}");

            String tmp = """
  if (json.containsKey('${key}')) {
    var tmp = json['${key}'];
    if (tmp is List) {
      ${key} = json['${key}'].cast<String>();
    } else {
      ${key} = [];
    }
  } else {
    ${key} = [];
  }
            """;

            list.add(tmp);
          }
        }
      } else if (value is bool) {
        //是bool
        //list.add("\t${key} = json.containsKey('${key}') ? json['${key}'] : false;");

        //       String str = """
        // if (json.containsKey('${key}')) {
        //   var tmp = json['${key}'];
        //   if (tmp is bool) {
        //     ${key} = json['${key}'];
        //   }else {
        //     ${key} = false;
        //   }
        // } else {
        //   ${key} = false;
        // }
        //       """;
        //       list.add(str);

        list.add("${key} = MyEntityLog.handleBool(json,'${key}');");
      } else {
        //其他是String
        // list.add("\t${key} = json.containsKey('${key}') ? '\${json['${key}']}' : '';");

        //       String str = """
        // if (json.containsKey('${key}')) {
        //   var tmp = json['${key}'];
        //   if (tmp is int || tmp is double || tmp is String) {
        //     ${key} = '\${json['${key}']}';
        //   } else {
        //     ${key} = '';
        //   }
        // } else {
        //   ${key} = '';
        // }
        //       """;
        //       list.add(str);

        list.add("${key} = MyEntityLog.handleString(json,'${key}');");
      }
    }
  }
  list.add("\n}\n");

  //生成toJson方法
  list.add("Map<String, dynamic> toJson() {");
  list.add("\tfinal Map<String, dynamic> data = new Map<String, dynamic>();");
  for (String key in map.keys) {
    var value = map[key];

    if (value != null) {
      if (value is Map) {
        list.add(
            "\tif (this.${key} != null) {data['${key}'] = this.${key}!.toJson();}");
      } else if (value is List) {
        var firstObj = value.first;
        if (firstObj is Map) {
          list.add(
              "if (this.${key} != null) {data['${key}'] = this.${key}!.map((v) => v.toJson()).toList();}");
        } else if (firstObj is List) {
        } else {
          list.add("data['${key}'] = this.${key};");
        }
      } else {
        list.add("\tdata['${key}'] = this.${key};");
      }
    }
  }
  list.add("return data;");
  list.add("\n}\n");

  list.add("}\n");

  for (var tmp in inList) {
    list.addAll(tmp);
  }

  return list;
}