Tutorials

Keep a denied page closed; return to an existing Catalog

Separate Guard denial from sign-in and cancelled selection; migrate navigate without duplicating Catalog.

v2.4.1Intermediate

This continues the picker and Account examples. You can run the complete applications from the fixture. Nothing in this chapter changes the published Ark Navigation runtime.

1. Try a product that must not open

Open Catalog and press Open restricted product. The product ID restricted is valid input, but this example's business rule forbids opening it. You stay on the same Catalog, with its current selection untouched. No Product page or Sign in page appears. Press Open product afterwards: the permitted product still opens and Select returns sku-42.

Repeat in Account → Catalog. The rule applies to this separate nested stack too. It does not close Account or switch tabs.

This differs from two other outcomes:

  • Redirect to Sign in chooses another destination because authentication is required. See the Account chapter.
  • Cancel selection closes an already opened Product and returns null to its waiting Catalog.
  • Guard denial never opens the Product. It must not produce a cancelled selection or replace an earlier selection with null.

2. Where each application denies the request

The button really attempts typed navigation; it does not replace the Guard with a permission check in the button handler.

  • GoSourceApp uses go_router's top-level onEnter. When the next Product ID is restricted, it returns Block.stop(). Authentication redirect remains separate.
  • AutoProductGuard calls resolver.next(false). The same guard is registered on guest and private Product declarations, not on the whole application.
  • ProductAccessGuard returns NavigationGuardDenied on entry. Leaving an allowed Product is not blocked. The route-level registration covers both Catalog mounts.

The fixture's SelectionRecorder records denials separately from picker results. The restricted-action handlers deliberately do not feed their nullable futures into the normal selection callback. In Ark, production callers can inspect ticket.committed to distinguish refusal from an opened page; completion of an opened page is a separate stage. A nullable picker future alone must not be treated as a universal operation-status contract across routers.

This small demo preserves the Catalog message on denial. It does not implement a snackbar, an authorization service, asynchronous permission refresh or an application-wide error presenter.

3. Return from Product without adding a second Catalog

For auto_route and Ark, Home has a second action: Navigate to catalog.

  1. Start at Home, where no Catalog entry exists.
  2. Press Navigate to catalog: one Catalog opens, sorted by price.
  3. Open Product.
  4. Press Return to catalog: Product closes and the original Catalog reappears. The awaiting picker receives null exactly once because its opened Product was popped without a selected value.
  5. Press platform Back: Home appears. There is no extra Catalog to pop.

The AutoRoute application calls the real generated context.router.navigate(AutoCatalogRoute(...)) from Home and context.router.root.navigate(AutoCatalogRoute(...)) from Product. See Home and Product. Here navigate reuses an existing matching route, popping entries above it; when it cannot find that route, it pushes one.

4. Make the corresponding Ark policy explicit

GuestCatalogAccess is an application helper, not a new Ark runtime method. It:

  1. Reads only the root stack.
  2. Searches backwards for a CatalogDestination in the guest Catalog mount. A private Catalog in another Scope is not a match.
  3. If found, calls popUntil with NavigationEntryIdMatcher for that exact entry.
  4. Otherwise pushes a typed CatalogDestination in the guest mount.

The existing entry keeps its parameters and Widget State; this policy is not "navigate to arbitrary new parameters". If your application needs parameter replacement or reconciliation, define and test that policy explicitly instead of silently assuming every router's navigate means the same thing.

No route-name strings, global router, stack reconstruction or automatic choice between replacement and removal is introduced. There is deliberately no such button in the private Product: returning to a guest Catalog is a guest-flow policy, not an implicit escape from every nested Navigator.

5. What the executable checks establish

guard_and_navigate_migration_test.dart contains eight Widget checks:

  • Six compare repeated denial in guest and private Catalog across all three routers. They require the same Catalog element and route identity, no Product or Sign in, no picker result, and a successful allowed selection afterwards.
  • Two compare navigate with the Ark policy. They require the same existing Catalog element and entry identity, one null picker result and Back to Home without a duplicate Catalog.

Run from the fixture directory:

shell
flutter test test/guard_and_navigate_migration_test.dart

These are Widget acceptance scenarios for the pinned source-router versions, not a proof of arbitrary Guard callback ordering or equivalence of every navigate argument. The separate browser job checks the existing cold-link and Back/Forward journeys; it does not turn these eight Widget checks into browser or physical-device results.