1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 06:18:49 +00:00

navigation_and_routing: a bunch of cleanup (#886)

Fail early on nullable context objects - no code paths allow null
Eliminate superfluous private function
Use a function over an abstract class with one method
This commit is contained in:
Kevin Moore
2021-08-26 16:14:29 -07:00
committed by GitHub
parent ad6dc454f2
commit ecf716dcab
13 changed files with 84 additions and 122 deletions

View File

@@ -48,8 +48,6 @@ class _BookstoreState extends State<Bookstore> {
@override
void initState() {
final guard = BookstoreRouteGuard(auth: _auth);
/// Configure the parser with all of the app's allowed path templates.
_routeParser = TemplateRouteParser(
allowedPaths: [
@@ -62,7 +60,7 @@ class _BookstoreState extends State<Bookstore> {
'/book/:bookId',
'/author/:authorId',
],
guard: guard,
guard: _guard,
initialRoute: '/signin',
);
@@ -97,6 +95,21 @@ class _BookstoreState extends State<Bookstore> {
),
);
Future<ParsedRoute> _guard(ParsedRoute from) async {
final signedIn = _auth.signedIn;
final signInRoute = ParsedRoute('/signin', '/signin', {}, {});
// Go to /signin if the user is not signed in
if (!signedIn && from != signInRoute) {
return signInRoute;
}
// Go to /books if the user is signed in and tries to go to /signin.
else if (signedIn && from == signInRoute) {
return ParsedRoute('/books/popular', '/books/popular', {}, {});
}
return from;
}
void _handleAuthStateChanged() {
if (!_auth.signedIn) {
_routeState.go('/signin');