Skip to content

Commit

Permalink
Fix directly navigating to unknown routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gilder authored and Tom Gilder committed May 31, 2021
1 parent 6d13bca commit dfb862b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.9.2

* Fixed: navigating to an unknown page on startup could throw an exception

# 0.9.1

* Fixed: issue where `routesBuilder` could be called outside the build phase.
Expand Down
8 changes: 6 additions & 2 deletions lib/routemaster.dart
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,13 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>
);

if (pages == null) {
final noCurrentPages = _state.stack._getCurrentPages().isEmpty;

// No page found from router
if (isRetry) {
// This is a retry after giving the routing map a chance to rebuild
if (isRetry || noCurrentPages) {
// Either we're retrying after giving the routing map a chance to
// rebuild, or we don't have a current stack of pages so we *have* to
// build immediately.
pages = _onUnknownRoute(request);
} else {
// No page has been found, but we don't call onUnknownRoute immediately.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: routemaster
description: Easy-to-use Navigator 2.0 router for web, mobile and desktop. URL-based routing, simple navigation of tabs and nested routes.
version: 0.9.1
version: 0.9.2
homepage: https://github.com/tomgilder/routemaster

environment:
Expand Down
44 changes: 44 additions & 0 deletions test/router_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:routemaster/routemaster.dart';
import 'package:routemaster/src/not_found_page.dart';
import 'helpers.dart';

void main() {
Expand Down Expand Up @@ -481,6 +482,49 @@ void main() {
expect(builderQueryParameters['query2'], '2');
expect(contextQueryParameters['query2'], '2');
});

testWidgets('Unknown startup URL shows not found page', (tester) async {
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: const RoutemasterParser(),
routeInformationProvider: PlatformRouteInformationProvider(
initialRouteInformation: const RouteInformation(location: '/404'),
),
routerDelegate: RoutemasterDelegate(
routesBuilder: (_) => RouteMap(
routes: {'/': (_) => const MaterialPageOne()},
),
),
),
);

expect(find.byType(DefaultNotFoundPage), findsOneWidget);
});

testWidgets('Unknown startup URL redirects to another page', (tester) async {
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: const RoutemasterParser(),
routeInformationProvider: PlatformRouteInformationProvider(
initialRouteInformation: const RouteInformation(location: '/404'),
),
routerDelegate: RoutemasterDelegate(
routesBuilder: (_) => RouteMap(
onUnknownRoute: (_) {
return const Redirect('/two');
},
routes: {
'/two': (_) {
return const MaterialPageTwo();
},
},
),
),
),
);

expect(find.byType(PageTwo), findsOneWidget);
});
}

class QueryParamEcho extends StatelessWidget {
Expand Down

0 comments on commit dfb862b

Please sign in to comment.