Skip to content

Commit

Permalink
## 1.4.0
Browse files Browse the repository at this point in the history
* Fix error when List is empty
* Add const ctor when properties are all final
  • Loading branch information
zmtzawqlp committed Aug 24, 2021
1 parent 4d8ed24 commit 91cc4dc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Flutter/json_to_dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

## 1.4.0

* Fix error when List is empty
* Add const ctor when properties are all final

## 1.3.0

* Add copy method button
Expand Down
26 changes: 16 additions & 10 deletions Flutter/json_to_dart/lib/models/dart_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,12 @@ class DartObject extends DartProperty {

final MyStringBuffer copySb = MyStringBuffer();

factorySb.writeLine(
stringFormat(DartHelper.factoryStringHeader, <String>[className]));
final bool isAllFinalProperties = !properties.any(
(DartProperty element) =>
element.propertyAccessorType != PropertyAccessorType.final_);

factorySb.writeLine(stringFormat(DartHelper.factoryStringHeader,
<String>['${isAllFinalProperties ? 'const' : ''} $className']));

toJsonSb.writeLine(DartHelper.toJsonHeader);

Expand Down Expand Up @@ -361,10 +365,12 @@ class DartObject extends DartProperty {
typeString += '?';
}

if (!ConfigSetting().nullsafety || item.nullable) {
copyProperty += '?';
if (ConfigSetting().addCopyMethod) {
if (!ConfigSetting().nullsafety || item.nullable) {
copyProperty += '?';
}
copyProperty += '.copy()';
}
copyProperty += '.copy()';
} else if (item.value is List) {
if (objectKeys.containsKey(item.key)) {
className = objectKeys[item.key]!.className;
Expand Down Expand Up @@ -392,8 +398,8 @@ class DartObject extends DartProperty {
}
}
setString += ',';

copyProperty = item.getListCopy(className: className);
if (ConfigSetting().addCopyMethod)
copyProperty = item.getListCopy(className: className);
} else {
setString = DartHelper.setProperty(item.name, item, this.className);
typeString = DartHelper.getDartTypeString(item.type, item);
Expand Down Expand Up @@ -443,8 +449,8 @@ class DartObject extends DartProperty {
item.key,
setName,
]));

copySb.writeLine('${item.name}:$copyProperty,');
if (ConfigSetting().addCopyMethod)
copySb.writeLine('${item.name}:$copyProperty,');
}

if (factorySb1.length == 0) {
Expand Down Expand Up @@ -483,7 +489,7 @@ class DartObject extends DartProperty {
sb.writeLine(DartHelper.classToString);
sb.writeLine(toJsonSb.toString());
if (ConfigSetting().addCopyMethod) {
sb.writeLine(stringFormat(DartHelper.copyWithString, <String>[
sb.writeLine(stringFormat(DartHelper.copyMethodString, <String>[
className,
copySb.toString(),
]));
Expand Down
4 changes: 2 additions & 2 deletions Flutter/json_to_dart/lib/models/dart_property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class DartProperty extends Equatable {
String copy = '';
String type = '{0}';

while (temp is List) {
while (temp is List && temp.isNotEmpty) {
if (copy == '') {
copy =
'e.map(($type e) => ${className != null ? 'e.copy()' : 'e'}).toList()';
} else {
type = 'List<$type>';
copy = 'e.map(($type e)=> $copy).toList()';
}
if (temp is List) {
if (temp is List && temp.isNotEmpty) {
temp = temp.first;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Flutter/json_to_dart/lib/utils/dart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ T? asT<T extends Object?>(dynamic value, [T? defaultValue]) {
return asTString;
}

static const String copyWithString = '''
static const String copyMethodString = '''
{0} copy() {
return {0}(
Expand Down
5 changes: 3 additions & 2 deletions Flutter/json_to_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ description: The tool to convert json to dart code.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.3.0
version: 1.4.0

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:


# collection: any
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
dart_style: ^2.0.0
Expand All @@ -34,6 +34,7 @@ dependencies:
oktoast: any
provider: any
tuple: any

dependency_overrides:
dart_style: ^2.0.0
dev_dependencies:
Expand Down

0 comments on commit 91cc4dc

Please sign in to comment.