Skip to content

Commit ca59c50

Browse files
ValentinVignalTecHaxter
authored andcommitted
[go_router] Use the correct configuration to build the state passed to the onExit (flutter#6623)
While working on flutter#6614, I notice some issues with the state that is given to the `onExit` (see the run https://github.com/flutter/packages/pull/6614/checks?check_run_id=24284539541) This PR uses the correct configuration to build the state and pass it to the `onExit`
1 parent 0cb67a7 commit ca59c50

File tree

4 files changed

+164
-7
lines changed

4 files changed

+164
-7
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 14.1.1
2+
3+
- Fixes correctness of the state provided in the `onExit`.
4+
15
## 14.1.0
26

37
- Adds route redirect to ShellRoutes

packages/go_router/lib/src/delegate.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
233233
exitingMatches.length - 1,
234234
context: navigatorContext,
235235
matches: exitingMatches,
236-
configuration: configuration,
237236
).then<void>((bool exit) {
238237
if (!exit) {
239238
return SynchronousFuture<void>(null);
@@ -254,7 +253,6 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
254253
int index, {
255254
required BuildContext context,
256255
required List<RouteMatch> matches,
257-
required RouteMatchList configuration,
258256
}) {
259257
if (index < 0) {
260258
return SynchronousFuture<bool>(true);
@@ -266,7 +264,6 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
266264
index - 1,
267265
context: context,
268266
matches: matches,
269-
configuration: configuration,
270267
);
271268
}
272269

@@ -276,15 +273,14 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
276273
index - 1,
277274
context: context,
278275
matches: matches,
279-
configuration: configuration,
280276
);
281277
}
282278
return SynchronousFuture<bool>(false);
283279
}
284280

285281
final FutureOr<bool> exitFuture = goRoute.onExit!(
286282
context,
287-
match.buildState(_configuration, configuration),
283+
match.buildState(_configuration, currentConfiguration),
288284
);
289285
if (exitFuture is bool) {
290286
return handleOnExitResult(exitFuture);

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
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: 14.1.0
4+
version: 14.1.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/on_exit_test.dart

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void main() {
244244
expect(await router.routerDelegate.popRoute(), false);
245245
});
246246

247-
testWidgets('It should provide the correct state to the onExit callback',
247+
testWidgets('It should provide the correct uri to the onExit callback',
248248
(WidgetTester tester) async {
249249
final UniqueKey home = UniqueKey();
250250
final UniqueKey page1 = UniqueKey();
@@ -314,4 +314,161 @@ void main() {
314314
expect(find.byKey(home), findsOneWidget);
315315
expect(onExitState1.uri.toString(), '/1');
316316
});
317+
318+
testWidgets(
319+
'It should provide the correct path parameters to the onExit callback',
320+
(WidgetTester tester) async {
321+
final UniqueKey page0 = UniqueKey();
322+
final UniqueKey page1 = UniqueKey();
323+
final UniqueKey page2 = UniqueKey();
324+
final UniqueKey page3 = UniqueKey();
325+
late final GoRouterState onExitState1;
326+
late final GoRouterState onExitState2;
327+
late final GoRouterState onExitState3;
328+
final List<GoRoute> routes = <GoRoute>[
329+
GoRoute(
330+
path: '/route-0/:id0',
331+
builder: (BuildContext context, GoRouterState state) =>
332+
DummyScreen(key: page0),
333+
),
334+
GoRoute(
335+
path: '/route-1/:id1',
336+
builder: (BuildContext context, GoRouterState state) =>
337+
DummyScreen(key: page1),
338+
onExit: (BuildContext context, GoRouterState state) {
339+
onExitState1 = state;
340+
return true;
341+
},
342+
),
343+
GoRoute(
344+
path: '/route-2/:id2',
345+
builder: (BuildContext context, GoRouterState state) =>
346+
DummyScreen(key: page2),
347+
onExit: (BuildContext context, GoRouterState state) {
348+
onExitState2 = state;
349+
return true;
350+
},
351+
),
352+
GoRoute(
353+
path: '/route-3/:id3',
354+
builder: (BuildContext context, GoRouterState state) {
355+
return DummyScreen(key: page3);
356+
},
357+
onExit: (BuildContext context, GoRouterState state) {
358+
onExitState3 = state;
359+
return true;
360+
},
361+
),
362+
];
363+
364+
final GoRouter router = await createRouter(
365+
routes,
366+
tester,
367+
initialLocation: '/route-0/0?param0=0',
368+
);
369+
unawaited(router.push('/route-1/1?param1=1'));
370+
unawaited(router.push('/route-2/2?param2=2'));
371+
unawaited(router.push('/route-3/3?param3=3'));
372+
373+
await tester.pumpAndSettle();
374+
expect(find.byKey(page3), findsOne);
375+
376+
router.pop();
377+
await tester.pumpAndSettle();
378+
expect(find.byKey(page2), findsOne);
379+
expect(onExitState3.uri.toString(), '/route-3/3?param3=3');
380+
expect(onExitState3.pathParameters, const <String, String>{'id3': '3'});
381+
expect(onExitState3.fullPath, '/route-3/:id3');
382+
383+
router.pop();
384+
await tester.pumpAndSettle();
385+
expect(find.byKey(page1), findsOne);
386+
expect(onExitState2.uri.toString(), '/route-2/2?param2=2');
387+
expect(onExitState2.pathParameters, const <String, String>{'id2': '2'});
388+
expect(onExitState2.fullPath, '/route-2/:id2');
389+
390+
router.pop();
391+
await tester.pumpAndSettle();
392+
expect(find.byKey(page0), findsOne);
393+
expect(onExitState1.uri.toString(), '/route-1/1?param1=1');
394+
expect(onExitState1.pathParameters, const <String, String>{'id1': '1'});
395+
expect(onExitState1.fullPath, '/route-1/:id1');
396+
},
397+
);
398+
399+
testWidgets(
400+
'It should provide the correct path parameters to the onExit callback during a go',
401+
(WidgetTester tester) async {
402+
final UniqueKey page0 = UniqueKey();
403+
final UniqueKey page1 = UniqueKey();
404+
final UniqueKey page2 = UniqueKey();
405+
final UniqueKey page3 = UniqueKey();
406+
late final GoRouterState onExitState0;
407+
late final GoRouterState onExitState1;
408+
late final GoRouterState onExitState2;
409+
final List<GoRoute> routes = <GoRoute>[
410+
GoRoute(
411+
path: '/route-0/:id0',
412+
builder: (BuildContext context, GoRouterState state) =>
413+
DummyScreen(key: page0),
414+
onExit: (BuildContext context, GoRouterState state) {
415+
onExitState0 = state;
416+
return true;
417+
},
418+
),
419+
GoRoute(
420+
path: '/route-1/:id1',
421+
builder: (BuildContext context, GoRouterState state) =>
422+
DummyScreen(key: page1),
423+
onExit: (BuildContext context, GoRouterState state) {
424+
onExitState1 = state;
425+
return true;
426+
},
427+
),
428+
GoRoute(
429+
path: '/route-2/:id2',
430+
builder: (BuildContext context, GoRouterState state) =>
431+
DummyScreen(key: page2),
432+
onExit: (BuildContext context, GoRouterState state) {
433+
onExitState2 = state;
434+
return true;
435+
},
436+
),
437+
GoRoute(
438+
path: '/route-3/:id3',
439+
builder: (BuildContext context, GoRouterState state) {
440+
return DummyScreen(key: page3);
441+
},
442+
),
443+
];
444+
445+
final GoRouter router = await createRouter(
446+
routes,
447+
tester,
448+
initialLocation: '/route-0/0?param0=0',
449+
);
450+
expect(find.byKey(page0), findsOne);
451+
452+
router.go('/route-1/1?param1=1');
453+
await tester.pumpAndSettle();
454+
expect(find.byKey(page1), findsOne);
455+
expect(onExitState0.uri.toString(), '/route-0/0?param0=0');
456+
expect(onExitState0.pathParameters, const <String, String>{'id0': '0'});
457+
expect(onExitState0.fullPath, '/route-0/:id0');
458+
459+
router.go('/route-2/2?param2=2');
460+
await tester.pumpAndSettle();
461+
expect(find.byKey(page2), findsOne);
462+
expect(onExitState1.uri.toString(), '/route-1/1?param1=1');
463+
expect(onExitState1.pathParameters, const <String, String>{'id1': '1'});
464+
expect(onExitState1.fullPath, '/route-1/:id1');
465+
466+
router.go('/route-3/3?param3=3');
467+
await tester.pumpAndSettle();
468+
expect(find.byKey(page3), findsOne);
469+
expect(onExitState2.uri.toString(), '/route-2/2?param2=2');
470+
expect(onExitState2.pathParameters, const <String, String>{'id2': '2'});
471+
expect(onExitState2.fullPath, '/route-2/:id2');
472+
},
473+
);
317474
}

0 commit comments

Comments
 (0)