Skip to content

Commit db270b4

Browse files
authored
[go_router] Allow users to specify onExit as optional (#11150)
Related to flutter/flutter#183099 Allow users to specify onExit as optional ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent fc795e5 commit db270b4

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

packages/go_router/lib/src/route_data.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,22 @@ class _GoRouteParameters {
9090
required this.builder,
9191
required this.pageBuilder,
9292
required this.redirect,
93-
required this.onExit,
93+
this.onExit,
9494
});
9595

9696
final GoRouterWidgetBuilder builder;
9797
final GoRouterPageBuilder pageBuilder;
9898
final GoRouterRedirect redirect;
99-
final ExitCallback onExit;
99+
final ExitCallback? onExit;
100100
}
101101

102102
/// Helper to create [GoRoute] parameters from a factory function and an Expando.
103+
///
104+
/// When [hasOverriddenOnExit] is null, treat it the same as true for backward compatibility.
103105
_GoRouteParameters _createGoRouteParameters<T extends _GoRouteDataBase>({
104106
required T Function(GoRouterState) factory,
105107
required Expando<_GoRouteDataBase> expando,
108+
bool? hasOverriddenOnExit,
106109
}) {
107110
T factoryImpl(GoRouterState state) {
108111
final Object? extra = state.extra;
@@ -123,8 +126,10 @@ _GoRouteParameters _createGoRouteParameters<T extends _GoRouteDataBase>({
123126
factoryImpl(state).buildPage(context, state),
124127
redirect: (BuildContext context, GoRouterState state) =>
125128
factoryImpl(state).redirect(context, state),
126-
onExit: (BuildContext context, GoRouterState state) =>
127-
factoryImpl(state).onExit(context, state),
129+
onExit: hasOverriddenOnExit == null || hasOverriddenOnExit
130+
? (BuildContext context, GoRouterState state) =>
131+
factoryImpl(state).onExit(context, state)
132+
: null,
128133
);
129134
}
130135

@@ -156,10 +161,12 @@ abstract class GoRouteData extends _GoRouteDataBase {
156161
required T Function(GoRouterState) factory,
157162
GlobalKey<NavigatorState>? parentNavigatorKey,
158163
List<RouteBase> routes = const <RouteBase>[],
164+
bool? hasOverriddenOnExit,
159165
}) {
160166
final _GoRouteParameters params = _createGoRouteParameters<T>(
161167
factory: factory,
162168
expando: _GoRouteDataBase.stateObjectExpando,
169+
hasOverriddenOnExit: hasOverriddenOnExit,
163170
);
164171

165172
return GoRoute(
@@ -227,10 +234,12 @@ abstract class RelativeGoRouteData extends _GoRouteDataBase {
227234
required T Function(GoRouterState) factory,
228235
GlobalKey<NavigatorState>? parentNavigatorKey,
229236
List<RouteBase> routes = const <RouteBase>[],
237+
bool? hasOverriddenOnExit,
230238
}) {
231239
final _GoRouteParameters params = _createGoRouteParameters<T>(
232240
factory: factory,
233241
expando: _GoRouteDataBase.stateObjectExpando,
242+
hasOverriddenOnExit: hasOverriddenOnExit,
234243
);
235244

236245
return GoRoute(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Adds `hasOverriddenOnExit` parameter to `GoRouteData.$route` and `RelativeGoRouteData.$route` helper methods for type-safe routes. When set to `true`, enables custom `onExit` callback invocation from route data classes extending `GoRouteData` or `RelativeGoRouteData` when the route is removed from the navigation stack.
3+
version: minor

0 commit comments

Comments
 (0)