NyRouteGuard class abstract

Base class for Nylo route guards.

Route guards intercept navigation and can:

  • Allow navigation to continue (next)
  • Redirect to a different route (redirect)
  • Abort navigation entirely (abort)

Lifecycle Hooks

  • onBefore: Called before navigation. Return next to continue.
  • onAfter: Called after successful navigation to the route.

Basic Example

class AuthGuard extends NyRouteGuard {
  @override
  Future<GuardResult> onBefore(RouteContext context) async {
    if (!await Auth.isLoggedIn()) {
      return redirect(LoginPage.path);
    }
    return next();
  }
}

With Data Transformation

class UserDataGuard extends NyRouteGuard {
  @override
  Future<GuardResult> onBefore(RouteContext context) async {
    final user = await UserService.getCurrentUser();
    // Enrich context with user data for the route
    setData(user);
    return next();
  }
}

Usage in Router

router.route(
  ProfilePage.path,
  (_) => ProfilePage(),
  routeGuards: [AuthGuard(), UserDataGuard()],
);
Inheritance
Implementers

Constructors

NyRouteGuard()

Properties

context BuildContext?
Convenience getter for the build context.
no setter
data → dynamic
Convenience getter for the route data.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasModifiedData bool
Whether setData was called during this guard's execution.
no setter
modifiedData → dynamic
Get modified data if setData was called.
no setter
pageRequest PageRequest?
getter/setter pairinherited
queryParameters Map<String, String>
Convenience getter for query parameters.
no setter
redirectConfig RedirectConfig?
Get the redirect configuration if a redirect was requested.
no setter
routeContext RouteContext?
The current route context.
no setter
routeName String?
Convenience getter for the route name.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

abort() GuardResult
Abort navigation without redirecting.
next() GuardResult
Continue to the next guard or to the route.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onAfter(RouteContext context) Future<void>
Called after successful navigation to the route.
onBefore(RouteContext context) Future<GuardResult>
Called before navigation to the route.
onRequest(PageRequest pageRequest) Future<PageRequest?>
Legacy method override for backward compatibility. Delegates to the new onBefore lifecycle method.
override
redirect(Object path, {dynamic data, Map<String, dynamic>? queryParameters, NavigationType navigationType = NavigationType.pushReplace, dynamic result, bool removeUntilPredicate(Route route)?, TransitionType? transitionType, dynamic onPop(dynamic value)?}) GuardResult
Redirect to a different route.
setData(dynamic data) → void
Modify the data passed to subsequent guards and the route.
setRouteContext(RouteContext context) → void
Set the route context. Called by the router.
toString() String
A string representation of this object.
inherited

Operators

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