Skip to content

Made generator set field to the initial value if null #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions json_serializable/lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,10 @@ void $toJsonMapHelperName(String key, dynamic value) {
// Generate the static factory method
//
buffer.writeln();
String objectName = '${className[0].toLowerCase()}${className.substring(1)}';
buffer
.writeln('$className ${prefix}FromJson(Map<String, dynamic> json) =>');
buffer.write(' new $className(');
.writeln('$className ${prefix}FromJson(Map<String, dynamic> json) {');
buffer.write(' $className $objectName = new $className(');
buffer.writeAll(
ctorArguments.map((paramElement) => _deserializeForField(
fields[paramElement.name], classSupportNullable,
Expand All @@ -402,17 +403,21 @@ void $toJsonMapHelperName(String key, dynamic value) {
ctorParam: paramElement)),
', ');

buffer.write(')');
if (fieldsToSet.isEmpty) {
buffer.writeln(';');
} else {
buffer.writeln(');');
if (fieldsToSet.isNotEmpty) {
buffer.write('$objectName');
for (var field in fieldsToSet.values) {
buffer.writeln();
buffer.write(' ..${field.name} = ');
buffer.write('..${field.name} = ');
buffer.write(_deserializeForField(field, classSupportNullable));
if (!_nullable(field, classSupportNullable)) {
buffer.write(' ?? $objectName.${field.name}');
}
}
buffer.writeln(';');
}
buffer.writeln('return $objectName;');
buffer.writeln('}');
buffer.writeln();

return finalFields;
Expand Down