Tutorials
Check a shared Catalog module and inspect retained tabs
A complete test of two mounts, an old product link and retained tab stacks, with setup and each assertion explained.
You have a Catalog feature available before and after login. The signed-in app also has Profile and Catalog tabs. Before connecting this configuration to screens, you want to answer four concrete questions:
- Can the graph be built without conflicting declarations?
- Does the same Product Destination resolve to the correct URL and Scope in both placements?
- Does an old product URL restore Catalog followed by Product, rather than just opening a page?
- After switching tabs, which entries are still retained?
This example answers all four with a runnable Flutter test. It does not open a window or require Material/Cupertino. It tests the navigation configuration, not authentication or visible screen layout.
1. Run the existing complete test
Open the package source at v2.4.0. From that repository's root, run:
flutter pub get
flutter test example/inspection_example_test.dart
The package repository already declares flutter_test. Installing only the package in an application does not copy this test file into your project. To adapt it there, use ark_navigation: ^2.4.1, add flutter_test from the Flutter SDK to dev_dependencies, and save the complete code below as test/navigation_inspection_test.dart. Then run flutter test test/navigation_inspection_test.dart.
A successful run finishes with one passing test and prints graph/session metadata. It does not start a graphical Inspector.
2. Understand the configuration
| Placement | Scope | Product's canonical URI |
|---|---|---|
| guest-catalog | root | /public/products/demo-product |
| account-catalog | catalog | /account/catalog/products/demo-product |
_Product carries a productId and returns a String when its page completes. Both placements use the same Catalog module; the requested Mount makes the intended placement explicit. Shell, Catalog and Profile are separate Destinations.
The page builder returns SizedBox deliberately: no UI is needed to verify routing. Real application widgets can use the same route contracts.
3. Read the checks in execution order
- builder.audit() checks the populated configuration before build. This example expects a valid report; when it is invalid, use formatAudit(report) to read findings and declaration locations. build() still validates its own input.
checkDestination<_Product, String>()runs once per Mount. It checks Route, Mount, Scope, canonical URI and the exact result type. sameProduct compares productId after encoding and decoding; a valid-looking URI alone would not catch an encoder that always used one fixed ID.checkUri<_Product, String>()takes the old alias /account/catalog/item/demo-product. It expects the canonical products URL and the full ordered plan: Catalog, then Product, both in the signed-in Mount.- controller.inspect() captures one immutable logical frame. The controller has pushed Shell, Catalog, Product and Profile, then selected the Catalog branch. The test expects two Catalog entries and one retained Profile entry.
The private ID used for the runtime Product does not appear in formatted metadata. The test checks that explicitly. It then pops Product and verifies that the earlier frame still has two Catalog entries: a frame is a historical value, not a live subscription.
4. Complete runnable code
The following is the versioned example source, including every helper and configuration declaration. No generated classes or omitted application services are required.
import 'package:ark_navigation/ark_navigation.dart';
import 'package:ark_navigation/inspection.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
/// Run with: flutter test example/inspection_example_test.dart
void main() {
test(
'audit two catalog mounts, restore an alias and inspect retained tabs',
() async {
final NavigationGraphBuilder builder = _Example.configuration();
final NavigationAuditReport structure = builder.audit();
expect(structure.isValid, isTrue);
final NavigationGraph graph = builder.build();
const NavigationRouteAuditor auditor = NavigationRouteAuditor();
// The same Destination type is valid in two explicitly selected placements.
for (final NavigationMountId mount in <NavigationMountId>[
_Example.guest,
_Example.signedIn,
]) {
final NavigationAuditReport route = auditor
.checkDestination<_Product, String>(
graph: graph,
destination: _Product('demo-product'),
mountId: mount,
expected: _Example.productExpectation(mount),
sameDestination: _Example.sameProduct,
);
expect(route.isValid, isTrue);
}
final NavigationAuditReport alias = auditor.checkUri<_Product, String>(
graph: graph,
uri: Uri.parse('/account/catalog/item/demo-product'),
expectedDestination: _Product('demo-product'),
expected: _Example.productExpectation(_Example.signedIn),
expectedPlan: <NavigationRouteExpectation>[
NavigationRouteExpectation(
routeId: _Example.catalogRoute,
mountId: _Example.signedIn,
scopeId: _Example.catalogScope,
canonicalUri: Uri.parse('/account/catalog'),
),
_Example.productExpectation(_Example.signedIn),
],
sameDestination: _Example.sameProduct,
);
expect(alias.isValid, isTrue);
final NavigationController controller = NavigationController(
sessionId: const NavigationSessionId('main'),
graph: graph,
);
addTearDown(controller.dispose);
await controller.push<void>(const _Shell()).committed;
await controller
.pushTarget<void>(
const NavigationTarget<void>(
destination: _Catalog(),
mountId: _Example.signedIn,
),
)
.committed;
await controller
.pushTarget<String>(
NavigationTarget<String>(
destination: _Product('customer-secret-42'),
mountId: _Example.signedIn,
),
)
.committed;
await controller.push<void>(const _Profile()).committed;
await controller.activateBranch(_Example.catalogScope);
final NavigationSessionInspection frame = controller.inspect();
expect(frame.logicalActiveScopeId, _Example.catalogScope);
expect(
frame.scopes
.firstWhere(
(final NavigationScopeInspection s) =>
s.id == _Example.catalogScope,
)
.entries,
hasLength(2),
);
expect(
frame.scopes
.firstWhere(
(final NavigationScopeInspection s) =>
s.id == _Example.profileScope,
)
.entries,
hasLength(1),
);
const NavigationInspectionFormatter formatter =
NavigationInspectionFormatter();
final String text = formatter.formatSession(frame);
expect(text, isNot(contains('customer-secret-42')));
expect(
text,
contains('logical stacks, not visibility or Back permission'),
);
print(
formatter.formatGraph(const NavigationInspector().captureGraph(graph)),
);
print(text);
// A one-shot frame is not changed by subsequent navigation or disposal.
await controller.pop(scopeId: _Example.catalogScope, result: 'selected');
expect(
frame.scopes
.firstWhere(
(final NavigationScopeInspection s) =>
s.id == _Example.catalogScope,
)
.entries,
hasLength(2),
);
},
);
}
final class _Shell extends NavigationDestination<void> {
const _Shell();
}
final class _Catalog extends NavigationDestination<void> {
const _Catalog();
}
final class _Profile extends NavigationDestination<void> {
const _Profile();
}
final class _Product extends NavigationDestination<String> {
_Product(this.productId) {
if (productId.trim().isEmpty) {
throw ArgumentError.value(productId, 'productId', 'Must not be blank.');
}
}
final String productId;
}
abstract final class _Example {
static const NavigationScopeId root = NavigationScopeId('root');
static const NavigationScopeId catalogScope = NavigationScopeId('catalog');
static const NavigationScopeId profileScope = NavigationScopeId('profile');
static const NavigationModuleId application = NavigationModuleId(
'application',
);
static const NavigationModuleId catalogModule = NavigationModuleId('catalog');
static const NavigationModuleId profileModule = NavigationModuleId('profile');
static const NavigationMountId guest = NavigationMountId('guest-catalog');
static const NavigationMountId signedIn = NavigationMountId(
'account-catalog',
);
static const NavigationRouteId catalogRoute = NavigationRouteId('catalog');
static const NavigationRouteId productRoute = NavigationRouteId('product');
static bool sameProduct(final _Product expected, final _Product restored) =>
expected.productId == restored.productId;
static Widget page<D>(
final BuildContext context,
final D destination,
final NavigationEntrySnapshot entry,
) => const SizedBox();
static NavigationRouteExpectation productExpectation(
final NavigationMountId mount,
) => NavigationRouteExpectation(
routeId: productRoute,
mountId: mount,
scopeId: mount == guest ? root : catalogScope,
canonicalUri: Uri.parse(
mount == guest
? '/public/products/demo-product'
: '/account/catalog/products/demo-product',
),
);
static NavigationGraphBuilder configuration() {
final NavigationGraphBuilder builder = NavigationGraphBuilder(
rootScopeId: root,
);
builder.scopes.addAll(const <NavigationScopeDefinition>[
NavigationScopeDefinition(id: root),
NavigationScopeDefinition(id: catalogScope, parentId: root),
NavigationScopeDefinition(id: profileScope, parentId: root),
]);
builder.modules.addAll(<NavigationModule>[
NavigationModule(
id: application,
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Shell, void>(
id: const NavigationRouteId('shell'),
pageBuilder: page<_Shell>,
uriCodec: CallbackNavigationUriCodec<_Shell>(
pattern: '/',
encoder: (final _Shell d) => Uri(path: '/'),
decoder: (final NavigationUriMatch m) => const _Shell(),
),
),
],
),
NavigationModule(
id: catalogModule,
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Catalog, void>(
id: catalogRoute,
pageBuilder: page<_Catalog>,
uriCodec: CallbackNavigationUriCodec<_Catalog>(
pattern: '/',
encoder: (final _Catalog d) => Uri(path: '/'),
decoder: (final NavigationUriMatch m) => const _Catalog(),
),
),
NavigationRouteDefinition<_Product, String>(
id: productRoute,
pageBuilder: page<_Product>,
uriCodec: CallbackNavigationUriCodec<_Product>(
pattern: '/products/:id',
encoder: (final _Product d) =>
Uri(pathSegments: <String>['products', d.productId]),
decoder: (final NavigationUriMatch m) => _Product(m.path('id')),
),
uriAliases: <NavigationUriAlias<_Product>>[
CallbackNavigationUriAlias<_Product>(
pattern: '/item/:id',
decoder: (final NavigationUriMatch m) => _Product(m.path('id')),
),
],
stackPlanBuilder: (final _Product d) => <NavigationTarget<Object?>>[
const NavigationTarget<void>(destination: _Catalog()),
NavigationTarget<String>(destination: d),
],
),
],
),
NavigationModule(
id: profileModule,
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Profile, void>(
id: const NavigationRouteId('profile'),
pageBuilder: page<_Profile>,
uriCodec: CallbackNavigationUriCodec<_Profile>(
pattern: '/',
encoder: (final _Profile d) => Uri(path: '/'),
decoder: (final NavigationUriMatch m) => const _Profile(),
),
),
],
),
]);
builder.mounts.addAll(<NavigationMount>[
NavigationMount(
id: const NavigationMountId('application'),
moduleId: application,
scopeId: root,
),
NavigationMount(
id: guest,
moduleId: catalogModule,
scopeId: root,
pathPrefix: '/public',
),
NavigationMount(
id: signedIn,
moduleId: catalogModule,
scopeId: catalogScope,
pathPrefix: '/account/catalog',
),
NavigationMount(
id: const NavigationMountId('profile'),
moduleId: profileModule,
scopeId: profileScope,
pathPrefix: '/account/profile',
),
]);
return builder;
}
}
5. Adapt it to your application
Keep the structure, but replace _Product, productId, the route IDs and both expected URIs with your own contracts. Add cases for parameter boundaries and every Mount your application exposes. Use an explicit expectedPlan for aliases that must restore parents; do not calculate the expected URI with the same encoder you are testing.
Keep these assertions in your test suite. To inspect an already running app, capture its actual controller rather than constructing another Session.
What this does not prove
A passing case does not check every product ID, login state, Guard or Redirect. These helpers do not run policies or page builders. A logical active branch is not proof of visible UI or Native Back permission. Metadata omits Destination values and concrete URIs, but application-chosen identifiers and patterns can still be sensitive.
Read Audit and Inspector for completeness flags, callback limits, custom Route metadata and capture budgets. Use Native Back for Back permission and Session and restoration for ongoing Trace events.