generateToStringMethod method

void generateToStringMethod(
  1. StringBuffer strBuffer,
  2. List<FieldInfo> fields
)

Implementation

void generateToStringMethod(StringBuffer strBuffer, List<FieldInfo> fields) {
  strBuffer.writeln("@override");
  strBuffer.writeln("String toString(){");
  strBuffer.writeln("  return '''");
  strBuffer.writeln("{");

  for (int i = 0; i < fields.length; i++) {
    var name = fields[i].name;
    var type = fields[i].type;
    var valStr = type == "String" ? '"\$$name"' : '\$$name';
    if (fields[i].isStringList) {
      valStr =
          '[\n      \${$name.map((e) => \'"\$e"\').join(",\\n      ")}\n    ]';
    }
    if (i != fields.length - 1) valStr += ",";
    strBuffer.writeln("  \"$name\" : $valStr");
  }

  strBuffer.writeln("}");
  strBuffer.writeln("''';");
  strBuffer.writeln("}");
}