Guard class

Provides functionality to block pages being loaded. Generally it's cleaner to not to use this class, and just use logic within the route map to return NotFound or Redirect, like this:

 '/protected-route': (route) {
   if (!isLoggedIn()) return Redirect('/login');
   if (!canUserAccessPage) return Redirect('/no-access');
   return ProtectedPage();
 }

If canNavigate returns true, the page returned by builder is shown.

If it returns false, the onNavigationFailed function is called. This can either return a new page, or return Redirect to go to a different path.

If no onNavigationFailed is provided, the default behavior is to redirect to the router's default path.

Inheritance

Constructors

Guard({required Page builder(), required bool canNavigate(RouteData info, BuildContext context), Page onNavigationFailed(RouteData info, BuildContext context)?})
Initializes a way to prevent loading of certain routes.
const

Properties

arguments Object?
The arguments passed to this route.
finalinherited
builder Page Function()
A function that returns a page to show if canNavigate returns true.
final
canNavigate bool Function(RouteData info, BuildContext context)
Callback to check if the route is valid. If this returns false, onNavigationFailed is called.
final
hashCode int
The hash code for this object.
no setterinherited
key LocalKey?
The key associated with this page.
finalinherited
name String?
The name of the route (e.g., "/settings").
finalinherited
onNavigationFailed → (Page Function(RouteData info, BuildContext context)?)
Callback, called when the canNavigate returns false.
final
restorationId String?
Restoration ID to save and restore the state of the Route configured by this page.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

canUpdate(Page other) bool
Whether this page can be updated with the other page.
inherited
createRoute(BuildContext context) Route
Creates the Route that corresponds to this page.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited