Skip to content

Commit 275eba6

Browse files
authored
Default to new, null-aware element feature in Dart 3.8 (#1498)
1 parent 83e7905 commit 275eba6

16 files changed

+74
-108
lines changed

_test_yaml/test/src/build_config.g.dart

Lines changed: 10 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ linter:
2929
- unnecessary_breaks
3030
- unnecessary_ignore
3131
- use_full_hex_values_for_flutter_colors
32+
- use_null_aware_elements
3233
- use_string_buffers

example/lib/example.g.dart

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 6.9.6-wip
22

33
- Move `package:collection` to a dev dependency.
4+
- Use new `null-aware element` feature in generated code.
45
- Require Dart 3.8
56

67
## 6.9.5

json_serializable/lib/src/constants.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ const converterOrKeyInstructions = r'''
1414
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
1515
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html''';
1616

17-
/// This package generates code that uses case statements, which were introduced
18-
/// in Dart 3.0.
19-
const supportedLanguageConstraint = '^3.0.0';
17+
/// This package generates code that uses null-aware elements, which were
18+
/// introduced in Dart 3.8.
19+
const supportedLanguageConstraint = '^3.8.0';

json_serializable/lib/src/encoder_helper.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/dart/element/nullability_suffix.dart';
77
import 'package:source_helper/source_helper.dart';
88

9-
import 'constants.dart';
109
import 'enum_utils.dart';
1110
import 'helper_core.dart';
1211
import 'type_helpers/generic_factory_helper.dart';
@@ -109,10 +108,9 @@ mixin EncodeHelper implements HelperCore {
109108
final keyExpression = safeNameAccess(field);
110109
final valueExpression = _serializeField(field, access);
111110

112-
final keyValuePair = _canWriteJsonWithoutNullCheck(field)
113-
? '$keyExpression: $valueExpression'
114-
: 'if ($valueExpression case final $generatedLocalVarName?) '
115-
'$keyExpression: $generatedLocalVarName';
111+
final maybeQuestion = _canWriteJsonWithoutNullCheck(field) ? '' : '?';
112+
113+
final keyValuePair = '$keyExpression: $maybeQuestion$valueExpression';
116114
return ' $keyValuePair,\n';
117115
}),
118116
)

json_serializable/test/annotation_version_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void main() {
3232

3333
group('language version', () {
3434
test('is less than required', () async {
35-
const sdkLowerBound = '2.12.0';
35+
const sdkLowerBound = '3.7.0';
3636
await _structurePackage(
3737
environment: const {'sdk': '^$sdkLowerBound'},
3838
dependencies: {'json_annotation': _annotationLowerBound},

json_serializable/test/integration/converter_examples.g.dart

Lines changed: 12 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/integration/create_per_field_to_json_example.g.dart

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/integration/json_enum_example.g.dart

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)