Recipes
Deep links and restoration
Encode typed parameters, reconstruct parent pages, accept legacy URLs, and separate restorable routes from process-local screens.
v1.1.0Intermediate
Deep links and restoration
Convert a typed Product to /products/42
dart
uriCodec: CallbackNavigationUriCodec<ProductDestination>(
pattern: '/products/:id',
encoder: (value) => Uri(path: '/products/${value.productId}'),
decoder: (match) => ProductDestination(match.path('id')),
),
Feature code continues to pass ProductDestination; it never parses the URI.
Restore parents for a direct Product link
dart
stackPlanBuilder: (product) => <NavigationTarget<Object?>>[
const NavigationTarget<Object?>(destination: HomeDestination()),
const NavigationTarget<Object?>(destination: CatalogDestination()),
NavigationTarget<Object?>(destination: product),
],
Direct result: [Home, Catalog, Product] instead of [Product].
Keep /item/42 working after moving to /products/42
Add a decode-only CallbackNavigationUriAlias<ProductDestination> with pattern /item/:id. Keep the canonical codec unchanged so generated links always use the new URL.
Keep a controller out of restoration
dart
NavigationRouteDefinition<PreviewDestination, void>(
id: const NavigationRouteId('preview'),
restorationPolicy: NavigationRestorationPolicy.sessionOnly,
pageBuilder: (context, destination, entry) => PreviewPage(destination.controller),
)
No URI codec is required. The Preview exists only until the process or Session ends.
Replace browser history during canonicalization
dart
navigation.replaceTop<void>(
canonicalDestination,
historyMode: NavigationHistoryMode.replace,
);
The browser Back button skips the obsolete address.