Navigation
Tracking Flutter routes as views and screen-load spans with SophonzNavigationObserver or sophonz_go_router, including StatefulShellRoute, route naming and screens that are not routes.
The native SDKs see a Flutter app as one activity or one view controller, so they cannot tell Flutter screens apart. Route tracking happens in Dart: a navigator observer turns each route change into a view and a screen-load span. Use SophonzNavigationObserver with Navigator and MaterialApp, and SophonzGoRouterObserver from sophonz_go_router with go_router.
What is recorded
| Record | Name | When |
|---|---|---|
| View | the route name | From the route becoming current until another route replaces it or it is popped. Recorded natively through startView / endView |
| Screen-load span | the route name | From the touch that triggered the navigation (or the push time) to the end of the next frame |
spz-time-to-interactive-flutter | fixed | Once per process, from Dart start to the frame after the first tracked route |
On Android a view is a view span in the Android SDK. On iOS it is a screen-view span with the route in its view name attribute.
NOTE — The screen-load span ends at the first frame
It measures how long the first frame of the new route took to appear, not how long the screen took to fill with data. For a screen that loads data after it appears, add your own span around the load. See Examples.
Screen-load start time
A route is usually pushed in response to a tap, and the tap is where the user starts waiting. The SDK records the time of the latest pointer down or up anywhere in the app. When a route is pushed, a touch no older than recencyThreshold becomes the span's start time and is consumed, so a later push cannot reuse it. Without a recent touch, for example a push after a network response, the start is the push time.
SophonzNavigationObserver(
screenLoadConfig: const SophonzScreenLoadConfig(
recencyThreshold: Duration(milliseconds: 500),
),
)The default threshold is one second.
Navigator and MaterialApp
MaterialApp(
navigatorObservers: [SophonzNavigationObserver()],
onGenerateRoute: onGenerateRoute,
);| Navigator event | View | Screen-load span |
|---|---|---|
push | ends the previous route's view, starts the new one | yes |
pushReplacement, replace | ends the replaced view, starts the new one | yes |
pop | ends the popped view, starts the view of the route below again | no |
The observer also tells frame monitoring the current route, so slow-frames and frozen-frame logs carry a route attribute.
Route names
A route is tracked only if it has a name. MaterialApp's home is named / and routes pushed with pushNamed are named after the path, but a MaterialPageRoute created without settings has no name and is skipped. The same applies to dialogs and bottom sheets unless you pass routeSettings.
Navigator.of(context).push(
MaterialPageRoute<void>(
settings: const RouteSettings(name: 'OrderDetail'),
builder: (_) => OrderDetailPage(orderId: order.id),
),
);Keep names low-cardinality. A name is used as a span name, so OrderDetail groups every order, while /orders/A-1042 makes one span name per order. Put IDs in attributes or session properties.
routeSettingsExtractor
The extractor returns the RouteSettings to record, or null to skip the route. Use it to rename routes, normalize paths, or ignore routes such as dialogs.
final idSegment = RegExp(r'/\d+');
SophonzNavigationObserver(
routeSettingsExtractor: (route) {
if (route is PopupRoute) return null; // dialogs, menus, bottom sheets
final name = route.settings.name;
if (name == null) return null;
return RouteSettings(name: name.replaceAll(idSegment, '/:id'));
},
)Nested navigators
A NavigatorObserver instance can be attached to only one Navigator. A nested Navigator, such as one per tab, needs its own SophonzNavigationObserver() in its observers. The time-to-interactive span is still recorded once per process.
go_router
sophonz_go_router supports go_router from 6.0.0 up to, but not including, 16.0.0. go_router 16 or newer does not resolve with v0.1.0. Add the package as described in Installation, including the dependency_overrides entry.
The observer has two modes. Pick one; do not use both on the same router.
| Observer mode | Router mode | |
|---|---|---|
| Set up with | GoRouter(observers: [SophonzGoRouterObserver()]) | SophonzGoRouterObserver(router: router) |
| Sees | routes on the root navigator | every location change, including ShellRoute and StatefulShellRoute branches |
| Name | RouteSettings.name, or routeSettingsExtractor | the location path (uri.path) |
Views (startView / endView) | yes | no |
| Screen-load span | yes | yes |
| Time to interactive | yes | yes |
| State span | no | spz-state-screen-flutter-automatic |
dispose() required | no | yes |
Observer mode
For apps without shell routes:
import 'package:go_router/go_router.dart';
import 'package:sophonz_go_router/sophonz_go_router.dart';
final router = GoRouter(
observers: [SophonzGoRouterObserver()],
routes: [
GoRoute(path: '/', builder: (_, __) => const HomePage()),
GoRoute(
path: '/orders/:id',
name: 'order',
builder: (_, state) => OrderPage(id: state.pathParameters['id']!),
),
],
);It behaves like SophonzNavigationObserver, except that it does not report the current route to frame monitoring. routeSettingsExtractor and screenLoadConfig work the same way.
Router mode (StatefulShellRoute)
GoRouter.observers only watches the root navigator. With StatefulShellRoute, switching branches does not push anything there, so observer mode misses it. Router mode listens to go_router's routeInformationProvider instead and sees every location change.
class App extends StatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
late final GoRouter _router = GoRouter(
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, shell) => ScaffoldWithNavBar(shell: shell),
branches: [
StatefulShellBranch(routes: [
GoRoute(path: '/home', builder: (_, __) => const HomePage()),
]),
StatefulShellBranch(routes: [
GoRoute(path: '/orders', builder: (_, __) => const OrdersPage()),
]),
],
),
],
);
// One instance for the router's lifetime. Creating it records the initial location.
late final SophonzGoRouterObserver _observer;
@override
void initState() {
super.initState();
_observer = SophonzGoRouterObserver(router: _router);
}
@override
void dispose() {
_observer.dispose();
_router.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => MaterialApp.router(routerConfig: _router);
}On construction the observer records the current location, then one screen-load span per change. It also keeps a state span, spz-state-screen-flutter-automatic, open for the router's lifetime:
| Attribute or event | Value |
|---|---|
spz.state.initial_value | the location when the observer was created |
transition event, spz.state.new_value | each new location |
spz.state.transition_count | number of transitions, set when dispose() ends the span |
CAUTION — Router mode names spans by the concrete path
The name is the location path as navigated, such as /orders/A-1042, not the route pattern. Paths with IDs produce one span name per ID, and routeSettingsExtractor is ignored in this mode. Prefer query parameters for IDs (/orders?id=A-1042 records as /orders), or use observer mode where naming matters more than branch coverage.
Router mode does not call startView / endView, so no view spans are recorded. If you need views per branch as well, call them yourself from the shell's branch change.
Call dispose() when the router goes away. Without it the listener stays attached and the state span never ends.
Screens that are not routes
Tabs in a TabBarView, pages in a PageView or an IndexedStack, and bottom sheets shown without a route do not go through a navigator. Record them as views directly:
class _HomeTabsState extends State<HomeTabs> with SingleTickerProviderStateMixin {
static const _names = ['Feed', 'Search', 'Profile'];
late final TabController _tabs = TabController(length: 3, vsync: this);
int _current = 0;
@override
void initState() {
super.initState();
Sophonz.instance.startView(_names[_current]);
_tabs.addListener(() {
if (_tabs.indexIsChanging || _tabs.index == _current) return;
Sophonz.instance.endView(_names[_current]);
_current = _tabs.index;
Sophonz.instance.startView(_names[_current]);
});
}
@override
void dispose() {
Sophonz.instance.endView(_names[_current]);
_tabs.dispose();
super.dispose();
}
// ...
}endView must use the same name as startView. On iOS, starting a view whose name is already open ends the earlier one first.
Platform notes
- iOS:
app.screen.nameis not updated. Flutter views and screen-load spans are recorded, but the Apple SDK's current-screen tracker only follows UIKit, so other spans keep the Flutter view controller's name. - iOS:
spz.typeon screen-load spans. The observers setspz.type: viewas an attribute. The Apple SDK sets the span type itself and discards a caller-suppliedspz.type, so on iOS screen-load spans are not typed as views. - Custom span limit. Screen-load spans are custom spans to the native SDK and share the per-session limit with your own spans.
Next
- Networking — HTTP capture and trace propagation
- API reference —
startViewandendView