Skip to content
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

[go_router_builder] Support ShellRouteData without const constructor #4627

Merged
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.2.5

* Fixes a bug where shell routes without const constructor were not generated correctly.

## 2.2.4

* Bumps example go_router version to v10.0.0 and migrate example code.
Expand Down
25 changes: 18 additions & 7 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ class ShellRouteConfig extends RouteBaseConfig {
final String? navigatorKey;

@override
Iterable<String> classDeclarations() => <String>[
'''
extension $_extensionName on $_className {
static $_className _fromState(GoRouterState state) => const $_className();
}
'''
];
Iterable<String> classDeclarations() {
if (routeDataClass.unnamedConstructor == null) {
throw InvalidGenerationSourceError(
'The ShellRouteData "$_className" class must have an unnamed constructor.',
element: routeDataClass,
);
}

final bool isConst = routeDataClass.unnamedConstructor!.isConst;

return <String>[
'''
extension $_extensionName on $_className {
static $_className _fromState(GoRouterState state) =>${isConst ? ' const' : ''} $_className();
}
'''
];
}

@override
String get routeConstructorParameters =>
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.2.4
version: 2.2.5
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
17 changes: 17 additions & 0 deletions packages/go_router_builder/test_inputs/shell_route_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:go_router/go_router.dart';

@TypedShellRoute<ShellRouteNoConstConstructor>(
routes: <TypedRoute<RouteData>>[],
)
class ShellRouteNoConstConstructor extends ShellRouteData {}

@TypedShellRoute<ShellRouteWithConstConstructor>(
routes: <TypedRoute<RouteData>>[],
)
class ShellRouteWithConstConstructor extends ShellRouteData {
const ShellRouteWithConstConstructor();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
RouteBase get $shellRouteNoConstConstructor => ShellRouteData.$route(
factory: $ShellRouteNoConstConstructorExtension._fromState,
);

extension $ShellRouteNoConstConstructorExtension
on ShellRouteNoConstConstructor {
static ShellRouteNoConstConstructor _fromState(GoRouterState state) =>
ShellRouteNoConstConstructor();
}

RouteBase get $shellRouteWithConstConstructor => ShellRouteData.$route(
factory: $ShellRouteWithConstConstructorExtension._fromState,
);

extension $ShellRouteWithConstConstructorExtension
on ShellRouteWithConstConstructor {
static ShellRouteWithConstConstructor _fromState(GoRouterState state) =>
const ShellRouteWithConstConstructor();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:go_router/go_router.dart';

@TypedShellRoute<ShellRouteWithoutUnnamedConstructor>(
routes: <TypedRoute<RouteData>>[],
)
class ShellRouteWithoutUnnamedConstructor extends ShellRouteData {
const ShellRouteWithoutUnnamedConstructor.namedConstructor();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ShellRouteData "ShellRouteWithoutUnnamedConstructor" class must have an unnamed constructor.