canActivate method

Future<bool> canActivate(
  1. Object componentInstance,
  2. RouterState? oldState,
  3. RouterState newState
)

Called by the router to indicate if a component canActivate.

The client should return a future that completes with the whether the componentInstance should be activated. If the component extends the CanActivate lifecycle, that will override this behavior.

You can use async in order to simplify when returning synchronously:

class MyHook extends RouterHook {
  final AuthService _authService;

  @override
  Future<bool> canActivate(
      Object component, RouterState _, RouterState __) async {
    // Make the default behavior to block all LoginRequired components
    // unless logged in.
    return _authService.isLoggedIn || component is! LoginRequired;
  }
}

Implementation

Future<bool> canActivate(Object componentInstance, RouterState? oldState,
    RouterState newState) async {
  // Provided as a default if someone extends or mixes-in this interface.
  return true;
}