Tutorials

Move a product picker from auto_route

Preserve typed results, stack ownership and explicit navigation behavior when replacing the root router.

v2.4.1Intermediate

For denied Product access and returning to an existing Catalog, continue with Guard and navigate.

This first case moves one complete journey: Home opens Catalog, Catalog opens Product, and Select returns the product ID. Cancel or platform Back returns null. The same page bodies run before and after migration.

For sign-in, retained tabs and logout, continue with the account migration walkthrough, which uses the same application. Continue with cold links and the browser job for URI restoration and actual Back/Forward/reload. This is a root-router replacement, not Ark nested inside a live auto_route owner.

1. Find and run the complete source

The fixture README describes the private comparison app. It lives in the repository, not in the published package archive. From the Ark Navigation repository checkout:

shell
cd tool/migration/source_routers
flutter pub get
flutter test test/picker_migration_test.dart

The official generated routes and lockfile are included in the checkout. Run dart run build_runner build after changing route declarations, not as a mandatory first-launch step. The comparison app is available in the release repository; its generators are not runtime dependencies of Ark Navigation.

Expected: 14 picker tests pass. They cover selection, button cancellation, platform Back and app disposal for each of three implementations, plus two blank-ID checks. Five additional CI tests check code excerpts and links, including the account walkthrough, in both guide languages.

The fixture requires Dart 3.13+ / Flutter 3.47+. This does not raise the main package's minimum SDK or add auto_route to Ark Navigation's dependencies.

2. Follow the existing call

Open AutoCatalogScreen. Its _open method currently starts with:

dart
final String? result = await context.router.push<String>(
  AutoProductRoute(productId: 'sku-42'),
);

productId is input. String? is the returned selection, not the route name. After awaiting the result, the still-mounted Catalog records it and displays either Selected: sku-42 or Cancelled.

The source Product route/page validates the ID and connects the common Select/Cancel buttons to the source router. No route is simulated by a fake API. The route object comes from auto_route_generator.

Do not replace every navigate call with push. This example transfers a push-and-return flow. Other stack rewrites need their own explicit decision; see the migration overview.

3. Declare the Ark input and result

The complete ProductDestination extends NavigationDestination<String>. Its constructor validates a nonblank product ID. It does not store BuildContext, a router, or a callback.

PickerGraph registers Home, Catalog and Product and connects them to the same page bodies. The Product URI codec owns encoding/decoding; the page receives the typed input. A typed parent plan defines the stack for direct guest Product URLs, but browser acceptance is not established by the picker interaction tests.

4. Change the caller, keeping the page

Open ArkCatalogScreen. The equivalent opening is:

dart
final NavigationTicket<String> ticket = ProductDestination('sku-42')
    .placed(mountId: widget.mountId)
    .push(context);
final NavigationOperationResult commit = await ticket.committed;

PickerGraph passes entry.mountId into ArkCatalogScreen. That placement is important now that the same Catalog Module is mounted in guest and private areas: the new Product belongs to the caller's Mount, not an arbitrary match.

If Catalog has been removed, stop. If commit is not NavigationCommitted, display Not opened; do not record a cancelled choice for a page that never opened. Once committed, await ticket.completed and check mounted again.

The complete switch in the linked source handles all outcomes:

OutcomeCatalog action
NavigationPopped with an IDRecord the ID once, display Selected
NavigationPopped with nullRecord one cancellation, display Cancelled
NavigationRemovedDisplay Flow removed; do not invent a selection
NavigationNotCommittedDisplay Not opened; do not invent a selection

The common ProductPage receives callbacks rather than owning a router. PickerGraph connects Select to context.readNavigation.pop(result: ...) and Cancel to pop without a result.

5. Give the application one owner

ArkTargetApp creates a NavigationSession once, passes its routerConfig to WidgetsApp.router and disposes the session with the app. MigrationApp selects one implementation at a time; it does not run two root routers in the same Flutter view.

Keep business data and long-lived services outside Destination. The fixture's SelectionRecorder simply makes callback delivery observable to the tests. It is not a new navigation abstraction or a recommended application state manager.

6. Verify the behavior you actually moved

In the executable tests:

  1. Browse, open Product, Select: Catalog shows the selected ID once.
  2. Repeat with Cancel: Catalog shows Cancelled, recorder contains one null.
  3. Repeat with a real platform navigation message: Back cancels the choice.
  4. Remove the app with Product open: no late callback and no framework error.
  5. Blank and whitespace-only product IDs are rejected at every typed boundary.

A mounted check is needed after each asynchronous wait, including commit. A disposed screen must not update UI just because the router completed a Future.

These are Widget interactions, not OS-link, real browser Back/Forward, interactive gesture or performance tests. Continue with modular migration for the already published Ark-only retained-stack and host-parent-plan examples.