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:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user