canDeactivate method

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

Called by the router to indicate if a component canDeactivate.

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

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

class MyHook extends RouterHook {
  final Window _window;

  @override
  Future<bool> canDeactivate(
      Object component, RouterState _, RouterState __) async {
    // Always ask if the user wants to navigate away from the page.
    return _window.confirm('Discard changes?');
  }
}

Implementation

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