toJson method

Map<String, dynamic> toJson({
  1. String? screen,
  2. String? route,
})

Generates the context JSON consumed by the external MCP server.

Formato:

{
  "screen": "LoginScreen",
  "route": "/login",
  "timestamp": "...",
  "widgets": [{ "key": "auth.login_button", "type": "ElevatedButton", ... }]
}

If screen is null, attempts to infer it from the first widget that has a defined screen.

Implementation

Map<String, dynamic> toJson({String? screen, String? route}) {
  final detectedScreen = screen ?? _detectCurrentScreen();
  return {
    'screen': detectedScreen,
    'route': route ?? '/${detectedScreen.toLowerCase()}',
    'timestamp': DateTime.now().toUtc().toIso8601String(),
    'widgets': _widgets.values.map((e) => e.metadata.toJson()).toList(),
  };
}