Skip to content

Commit

Permalink
[go_router] Fixes crashes when using async redirect. (#2916)
Browse files Browse the repository at this point in the history
* [go_router] Fixes crashes when using async redirect.

* update
  • Loading branch information
chunhtai committed Dec 9, 2022
1 parent aa0d1f2 commit 61771b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
@@ -1,3 +1,7 @@
## 5.2.4

- Fixes crashes when using async redirect.

## 5.2.3

- Fixes link for router configuration and sub-routes
Expand Down
5 changes: 4 additions & 1 deletion packages/go_router/lib/src/parser.dart
Expand Up @@ -98,7 +98,10 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {

/// for use by the Router architecture as part of the RouteInformationParser
@override
RouteInformation restoreRouteInformation(RouteMatchList configuration) {
RouteInformation? restoreRouteInformation(RouteMatchList configuration) {
if (configuration.isEmpty) {
return null;
}
if (configuration.matches.last is ImperativeRouteMatch) {
configuration =
(configuration.matches.last as ImperativeRouteMatch).matches;
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 5.2.3
version: 5.2.4
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
40 changes: 40 additions & 0 deletions packages/go_router/test/go_router_test.dart
Expand Up @@ -4,6 +4,8 @@

// ignore_for_file: cascade_invocations, diagnostic_describe_all_properties

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -1009,6 +1011,44 @@ void main() {
}),
]);
});

testWidgets('works correctly with async redirect',
(WidgetTester tester) async {
final UniqueKey login = UniqueKey();
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (_, __) => const DummyScreen(),
),
GoRoute(
path: '/login',
builder: (_, __) => DummyScreen(key: login),
),
];
final Completer<void> completer = Completer<void>();
await createRouter(routes, tester, redirect: (_, __) async {
await completer.future;
return '/login';
});
await tester.pumpAndSettle();
expect(find.byKey(login), findsNothing);
expect(tester.takeException(), isNull);
expect(log, <Object>[]);

completer.complete();
await tester.pumpAndSettle();

expect(find.byKey(login), findsOneWidget);
expect(tester.takeException(), isNull);
expect(log, <Object>[
isMethodCall('selectMultiEntryHistory', arguments: null),
isMethodCall('routeInformationUpdated', arguments: <String, dynamic>{
'location': '/login',
'state': null,
'replace': false
}),
]);
});
});

group('named routes', () {
Expand Down

0 comments on commit 61771b3

Please sign in to comment.