|
| 1 | +# Using navigation_devtools in your app |
| 2 | + |
| 3 | +A DevTools panel that shows your app's **live navigation stack**, **route tree + graph**, |
| 4 | +route **params/args**, and lets you **navigate interactively** — for `auto_route`, `go_router`, |
| 5 | +`Navigator 1.0`, or any custom `RouterDelegate`. |
| 6 | + |
| 7 | +It's **inert in release builds** (all instrumentation is stripped), so it's safe to leave in. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 1. Add the dependency |
| 12 | + |
| 13 | +Pick the one package that matches your router: |
| 14 | + |
| 15 | +| Your router | Add this | |
| 16 | +|---|---| |
| 17 | +| **auto_route** | `flutter pub add auto_route_devtools` | |
| 18 | +| **go_router** | `flutter pub add go_router_devtools` | |
| 19 | +| **Navigator 1.0** or custom `RouterDelegate` | `flutter pub add navigation_devtools` | |
| 20 | + |
| 21 | +(The adapter packages pull in `navigation_devtools` for you.) |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## 2. Wire it up (one call in `main`) |
| 26 | + |
| 27 | +### auto_route |
| 28 | + |
| 29 | +```dart |
| 30 | +import 'package:auto_route_devtools/auto_route_devtools.dart'; |
| 31 | +
|
| 32 | +void main() { |
| 33 | + final router = AppRouter(); |
| 34 | + AutoRouteDevTools.init(router); // 1. register |
| 35 | +
|
| 36 | + runApp(MaterialApp.router( |
| 37 | + routerConfig: router.config( |
| 38 | + navigatorObservers: () => [ |
| 39 | + AutoRouteDevTools.navigationObserver(), // 2. observe live changes |
| 40 | + ], |
| 41 | + ), |
| 42 | + )); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### go_router |
| 47 | + |
| 48 | +```dart |
| 49 | +import 'package:go_router/go_router.dart'; |
| 50 | +import 'package:go_router_devtools/go_router_devtools.dart'; |
| 51 | +import 'package:navigation_devtools/navigation_devtools.dart'; |
| 52 | +
|
| 53 | +void main() { |
| 54 | + final router = GoRouter(routes: [/* ... */]); |
| 55 | + NavigationDevTools.register(GoRouterAdapter(router)); // that's it |
| 56 | +
|
| 57 | + runApp(MaterialApp.router(routerConfig: router)); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Navigator 1.0 (routes map) |
| 62 | + |
| 63 | +```dart |
| 64 | +import 'package:navigation_devtools/navigation_devtools.dart'; |
| 65 | +
|
| 66 | +void main() { |
| 67 | + final adapter = Navigator1Adapter(routes: appRoutes); // your Map<String, WidgetBuilder> |
| 68 | + NavigationDevTools.register(adapter); |
| 69 | +
|
| 70 | + runApp(MaterialApp( |
| 71 | + routes: appRoutes, |
| 72 | + navigatorObservers: [adapter.observer], // observe live changes |
| 73 | + )); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Custom RouterDelegate |
| 78 | + |
| 79 | +```dart |
| 80 | +import 'package:navigation_devtools/navigation_devtools.dart'; |
| 81 | +
|
| 82 | +NavigationDevTools.register( |
| 83 | + RouterDelegateAdapter<AppPath>(delegate, parser: routeParser), |
| 84 | +); |
| 85 | +``` |
| 86 | + |
| 87 | +You can `register(...)` **multiple routers** — the panel shows a dropdown to switch between them. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 3. Open the panel |
| 92 | + |
| 93 | +1. Run your app (`flutter run`), then open **DevTools** (the link is printed in the console, |
| 94 | + or use the IDE's "Open DevTools" action). |
| 95 | +2. Click the **navigation_devtools** tab. |
| 96 | + - First time: enable it when DevTools asks (or via the ⚙️ Extensions setting). |
| 97 | + |
| 98 | +You'll see: the live **backstack** on the left; a **Tree / Graph** view of your routes on the |
| 99 | +right (tap a route to navigate to it — parameterized `:id` routes prompt for values); and a |
| 100 | +**detail** pane with the selected route's params and args at the bottom. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Notes |
| 105 | + |
| 106 | +- Requires a **debug or profile** build with DevTools attached — nothing runs in release. |
| 107 | +- Interactive navigation is **confirm-gated** by default (a dialog before it navigates); pass your |
| 108 | + own `confirm` callback to the panel if you embed it yourself. |
| 109 | +- Not every router exposes everything (e.g. a bare `Navigator 1.0` app has no static route table) — |
| 110 | + the panel adapts to each router's capabilities and hides what isn't available. |
0 commit comments