Skip to content

Commit

Permalink
fixed withNullability deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeiffer committed Jan 22, 2025
1 parent 16b622e commit 886972f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/codegen/lib/json_dynamic_widget_codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export 'src/builder/json_widget_registrar_builder.dart';
export 'src/decoder/decoders.dart';
export 'src/decoder/schema_decoders.dart';
//
export 'src/extension/dart_type_extension.dart';
//
export 'src/util/widget_metadata.dart';
12 changes: 3 additions & 9 deletions packages/codegen/lib/src/decoder/decoders.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:json_dynamic_widget_codegen/src/extension/dart_type_extension.dart';
import 'package:json_theme/codegen.dart';

typedef ParameterDecoder = String Function(
Expand Down Expand Up @@ -219,8 +218,7 @@ String decode(
if (defaultValueCode != null) {
if (defaultValueCode == 'null') {
defaultValueCode = null;
} else if (element.type.getDisplayString(withNullability: false) ==
'double') {
} else if (element.type.toNonNullableString() == 'double') {
if (int.tryParse(defaultValueCode) != null) {
defaultValueCode = '$defaultValueCode.0';
}
Expand All @@ -229,7 +227,7 @@ String decode(
var result =
"map['$name']${defaultValueCode == null ? '' : '?? $defaultValueCode'}";

final typeStr = element.type.getDisplayString(withNullability: false);
final typeStr = element.type.toNonNullableString();

var decoded = false;
if (paramDecoders.contains(name)) {
Expand Down Expand Up @@ -312,7 +310,3 @@ String _widgetDecoder(
return parsed;
}()
''';

extension DartTypeNullable on DartType {
bool get nullable => nullabilitySuffix == NullabilitySuffix.question;
}
9 changes: 2 additions & 7 deletions packages/codegen/lib/src/encoder/encoders.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:json_dynamic_widget_codegen/json_dynamic_widget_codegen.dart';
import 'package:json_theme/codegen.dart';

typedef ParameterEncoder = String Function(
Expand Down Expand Up @@ -90,7 +89,7 @@ String encode(
final displayName = element.name;
final name = aliases[element.name] ?? element.name;

final typeStr = element.type.getDisplayString(withNullability: false);
final typeStr = element.type.toNonNullableString();

final encoder = kEncoders[typeStr];
var result = displayName;
Expand All @@ -111,7 +110,3 @@ String encode(
'${element.name}': ${defaultValueCode == null ? result : '$defaultValueCode == $displayName ? null : $result'},
''';
}

extension DartTypeNullable on DartType {
bool get nullable => nullabilitySuffix == NullabilitySuffix.question;
}
16 changes: 16 additions & 0 deletions packages/codegen/lib/src/extension/dart_type_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';

extension DartTypeNullability on DartType {
bool get nullable => nullabilitySuffix == NullabilitySuffix.question;

String toNonNullableString() {
var result = getDisplayString();

if (nullabilitySuffix == NullabilitySuffix.question) {
result = result.substring(0, result.length - 1);
}

return result;
}
}

0 comments on commit 886972f

Please sign in to comment.