Skip to content

Commit 33554ba

Browse files
authored
Fix an issue where GoRoutes with only a redirect were disallowed. (#2620)
1 parent 9247f3e commit 33554ba

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

packages/go_router/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.5.1
2+
3+
- Fixes an issue where GoRoutes with only a redirect were disallowed
4+
(flutter/flutter#111763)
5+
16
## 4.5.0
27

38
- Adds ShellRoute for nested navigation support (flutter/flutter#99126)

packages/go_router/lib/src/redirection.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'configuration.dart';
66
import 'logging.dart';
77
import 'match.dart';
88
import 'matching.dart';
9+
import 'typedefs.dart';
910

1011
/// A GoRouter redirector function.
1112
// TODO(johnpryan): make redirector async
@@ -76,7 +77,12 @@ RouteMatchList redirect(RouteMatchList prevMatchList,
7677
assert(topRoute is GoRoute,
7778
'Last RouteMatch should contain a GoRoute, but was ${topRoute.runtimeType}');
7879
final GoRoute topGoRoute = topRoute as GoRoute;
79-
final String? topRouteLocation = topGoRoute.redirect(
80+
final GoRouterRedirect? redirect = topGoRoute.redirect;
81+
if (redirect == null) {
82+
break;
83+
}
84+
85+
final String? topRouteLocation = redirect(
8086
GoRouterState(
8187
configuration,
8288
location: currentMatches.location.toString(),

packages/go_router/lib/src/route.dart

+4-19
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,12 @@ class GoRoute extends RouteBase {
124124
this.builder,
125125
this.pageBuilder,
126126
this.parentNavigatorKey,
127-
this.redirect = _emptyRedirect,
127+
this.redirect,
128128
List<RouteBase> routes = const <RouteBase>[],
129129
}) : assert(path.isNotEmpty, 'GoRoute path cannot be empty'),
130130
assert(name == null || name.isNotEmpty, 'GoRoute name cannot be empty'),
131-
assert(!(builder == null && pageBuilder == null),
132-
'builder or pageBuilder must be provided'),
133-
assert(
134-
pageBuilder != null ||
135-
builder != _invalidBuilder ||
136-
redirect != _noRedirection,
137-
'GoRoute builder parameter not set\n'),
131+
assert(pageBuilder != null || builder != null || redirect != null,
132+
'builder, pageBuilder, or redirect must be provided'),
138133
super._(
139134
routes: routes,
140135
) {
@@ -288,7 +283,7 @@ class GoRoute extends RouteBase {
288283
/// routes, also known as route guards. One canonical example is user
289284
/// authentication. See [Redirection](https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/redirection.dart)
290285
/// for a complete runnable example.
291-
final GoRouterRedirect redirect;
286+
final GoRouterRedirect? redirect;
292287

293288
/// An optional key specifying which Navigator to display this route's screen
294289
/// onto.
@@ -305,19 +300,9 @@ class GoRoute extends RouteBase {
305300
Map<String, String> extractPathParams(RegExpMatch match) =>
306301
extractPathParameters(_pathParams, match);
307302

308-
static String? _emptyRedirect(GoRouterState state) => null;
309-
310303
final List<String> _pathParams = <String>[];
311304

312305
late final RegExp _pathRE;
313-
314-
static String? _noRedirection(GoRouterState state) => null;
315-
316-
static Widget _invalidBuilder(
317-
BuildContext context,
318-
GoRouterState state,
319-
) =>
320-
const SizedBox.shrink();
321306
}
322307

323308
/// A route that displays a UI shell around the matching child route.

packages/go_router/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 4.5.0
4+
version: 4.5.1
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/go_route_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ void main() {
1313
test('throws when a path is empty', () {
1414
expect(() => GoRoute(path: ''), throwsA(isAssertionError));
1515
});
16+
17+
test('does not throw when only redirect is provided', () {
18+
GoRoute(path: '/', redirect: (_) => '/a');
19+
});
1620
}

0 commit comments

Comments
 (0)