Skip to content

[go_router_builder] Fixes an deprecated warning for using withNullability #9158

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.8.3

- Fixes an deprecated warning for using withNullability

## 2.8.2

- Fixes an issue when enum params are not required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import 'route_config.dart';
import 'type_helpers.dart';

const String _routeDataUrl = 'package:go_router/src/route_data.dart';

Expand Down Expand Up @@ -79,7 +80,7 @@ ${getters.map((String e) => "$e,").join('\n')}
ConstantReader annotation,
) {
final String typedAnnotation =
annotation.objectValue.type!.getDisplayString(withNullability: false);
annotation.objectValue.type!.getDisplayString().withoutNullability;
final String type =
typedAnnotation.substring(0, typedAnnotation.indexOf('<'));
final String routeData = _annotations[type]!;
Expand Down
3 changes: 2 additions & 1 deletion packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class StatefulShellBranchConfig extends RouteBaseConfig {

@override
String get factorConstructorParameters => '';

@override
String get routeConstructorParameters =>
'${navigatorKey == null ? '' : 'navigatorKey: $navigatorKey,'}'
Expand Down Expand Up @@ -609,7 +610,7 @@ abstract class RouteBaseConfig {
return false;
}
final DartType typeArgument = typeArguments.single;
if (typeArgument.getDisplayString(withNullability: false) !=
if (typeArgument.getDisplayString().withoutNullability !=
'NavigatorState') {
return false;
}
Expand Down
15 changes: 13 additions & 2 deletions packages/go_router_builder/lib/src/type_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ String decodeParameter(ParameterElement element, Set<String> pathParameters) {

throw InvalidGenerationSourceError(
'The parameter type '
'`${paramType.getDisplayString(withNullability: false)}` is not supported.',
'`${paramType.getDisplayString().withoutNullability}` is not supported.',
element: element,
);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ String enumMapName(InterfaceType type) => '_\$${type.element.name}EnumMap';
String _stateValueAccess(ParameterElement element, Set<String> pathParameters) {
if (element.isExtraField) {
// ignore: avoid_redundant_argument_values
return 'extra as ${element.type.getDisplayString(withNullability: true)}';
return 'extra as ${element.type.getDisplayString()}';
}

late String access;
Expand Down Expand Up @@ -438,6 +438,17 @@ extension ParameterElementExtension on ParameterElement {
bool get isExtraField => name == extraFieldName;
}

/// Extension helpers on [String] for nullability check.
extension TypeStringExtensions on String {
/// Returns `true` if the type string ends with a nullability marker (`?` or `*`)
bool get _isNullableType => endsWith('?') || endsWith('*');

/// Returns the type string without a trailing nullability marker
String get withoutNullability {
return _isNullableType ? substring(0, length - 1) : this;
}
}

/// An error thrown when a default value is used with a nullable type.
class NullableDefaultValueError extends InvalidGenerationSourceError {
/// An error thrown when a default value is used with a nullable type.
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: go_router_builder
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
version: 2.8.2
version: 2.8.3
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22

Expand Down