triggerRouteNavigation function
Pushes the named route in args routePath.
Walks the element tree for a context below a Navigator. The route must
already be registered in the app. Returns an error result when it is not.
Implementation
Future<CallToolResult> triggerRouteNavigation(
VmBridge bridge,
Map<String, dynamic> args,
) async {
try {
final routePath = args['routePath'] as String?;
if (routePath == null || routePath.isEmpty) {
return CallToolResult(
content: const [TextContent(text: '`routePath` is required.')],
isError: true,
);
}
final quoted = "'${routePath.replaceAll("'", r"\'")}'";
var lastFailure = 'unknown';
for (final template in [_pushViaTreeWalk, _pushViaNavigatorKey]) {
final response = await bridge.evaluate(
template.replaceAll('ROUTE', quoted),
);
final out = response is ErrorRef
? 'eval-error: ${response.message ?? "unknown"}'
: (response as InstanceRef).valueAsString ?? '';
if (out == 'ok') {
return CallToolResult(
content: [TextContent(text: 'Pushed route "$routePath".')],
);
}
// A push failure means we reached a Navigator but it rejected the route.
// Trying the other strategy would only mask the useful error, so report
// this one and stop.
if (out.startsWith('push-failed')) {
return CallToolResult(
content: [
TextContent(
text:
'Could not push route "$routePath". The Navigator '
'rejected it — the route is most likely not registered in '
'the app\'s routes table.\n\n$out',
),
],
isError: true,
);
}
// No navigator reachable this way; try the next strategy.
lastFailure = out;
}
return CallToolResult(
content: [
TextContent(text: 'Could not navigate to "$routePath".\n$lastFailure'),
],
isError: true,
);
} catch (e) {
return CallToolResult(
content: [TextContent(text: 'Error navigating: $e')],
isError: true,
);
}
}