Skip to content

Commit

Permalink
add clone method
Browse files Browse the repository at this point in the history
  • Loading branch information
zmtzawqlp committed Apr 3, 2021
1 parent 3f3e303 commit 0fa95ed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Flutter/json_to_dart/lib/models/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConfigSetting extends Setting<ConfigSetting> with ChangeNotifier {
}

@HiveField(0)
bool addMethod = false;
bool addMethod = true;
@HiveField(1)
int column1Width = 2;
@HiveField(2)
Expand Down
14 changes: 9 additions & 5 deletions Flutter/json_to_dart/lib/models/dart_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,13 @@ class DartObject extends DartProperty {
DartHelper.propertyS(item.propertyAccessorType),
<String>[typeString, name, lowName]));
fromJsonSb.writeLine(setString);
toJsonSb.writeLine(stringFormat(
DartHelper.toJsonSetString, <String>[item.key, setName]));

final bool nonNullAble = ConfigSetting().nullsafety && !item.nullable;
toJsonSb.writeLine(stringFormat(DartHelper.toJsonSetString, <String>[
item.key,
setName +
(item is DartObject ? '${nonNullAble ? '' : '?'}.toJson()' : '')
]));
}

if (factorySb1.length == 0) {
Expand Down Expand Up @@ -355,10 +360,9 @@ class DartObject extends DartProperty {
sb.writeLine(factorySb.toString());
sb.writeLine(fromJson);
sb.writeLine(propertySb.toString());

sb.writeLine(toJsonSb.toString());

sb.writeLine(DartHelper.classToString);
sb.writeLine(toJsonSb.toString());
sb.writeLine(stringFormat(DartHelper.classToClone, <String>[className]));
}

sb.writeLine(DartHelper.classFooter);
Expand Down
7 changes: 4 additions & 3 deletions Flutter/json_to_dart/lib/utils/dart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ import 'dart:convert';''';
}

static const String classToString =
' @override\nString toString() {\n return jsonEncode(this);\n }';
' \n@override\nString toString() {\n return jsonEncode(this);\n }';

static const String classToClone =
'\n{0} clone() => {0}.fromJson(toJson());\n';

static DartType converDartType(Type type) {
if (type == int) {
Expand Down Expand Up @@ -133,7 +136,6 @@ T asT<T>(dynamic value) {
if (value is T) {
return value;
}
return null;
}
''';
Expand All @@ -143,7 +145,6 @@ T? asT<T>(dynamic value) {
if (value is T) {
return value;
}
return null;
}
''';
Expand Down

0 comments on commit 0fa95ed

Please sign in to comment.