getDiagnosticTree method

Future<RPCResponse> getDiagnosticTree({
  1. bool isSummaryTree = true,
  2. bool withPreviews = false,
  3. bool fullDetails = false,
})

Gets the diagnostic tree for the current Flutter widget tree. Returns a RemoteDiagnosticsNode representing the root of the tree. Each node contains:

  • description: Description of the widget/element
  • children: List of child nodes
  • properties: List of diagnostic properties
  • style: The style to use when displaying the node

Implementation

Future<RPCResponse> getDiagnosticTree({
  final bool isSummaryTree = true,
  final bool withPreviews = false,
  final bool fullDetails = false,
}) async {
  final serviceManager = devtoolsService.serviceManager;
  if (!serviceManager.connectedState.value.connected) {
    return RPCResponse.error('Not connected to VM service');
  }

  final vmService = serviceManager.service;
  if (vmService == null) {
    return RPCResponse.error('VM service not available');
  }

  final isolateId = serviceManager.isolateManager.mainIsolate.value?.id;
  if (isolateId == null) {
    return RPCResponse.error('No main isolate available');
  }
  final objectGroupManager = initObjectGroup(debugName: 'playground');

  try {
    // Get a new object group for this operation
    final group = objectGroupManager.next;

    try {
      // Use the appropriate extension based on parameters
      final extensionMethod =
          isSummaryTree
              ? withPreviews
                  ? WidgetInspectorServiceExtensions
                      .getRootWidgetSummaryTreeWithPreviews
                  : WidgetInspectorServiceExtensions.getRootWidgetSummaryTree
              : WidgetInspectorServiceExtensions.getRootWidgetTree;

      final response = await vmService.callServiceExtension(
        'ext.flutter.inspector.${extensionMethod.name}',
        isolateId: isolateId,
        args: {
          'objectGroup': group.groupName,
          if (withPreviews) 'includeProperties': 'true',
          if (fullDetails) 'subtreeDepth': '-1',
        },
      );

      if (response.json == null || response.json!['result'] == null) {
        await objectGroupManager.cancelNext();
        return RPCResponse.error('Root widget tree not available');
      }

      // Parse the root node
      final rootNode = RemoteDiagnosticsNode(
        response.json!['result'] as Map<String, Object?>,
        null, // objectGroupApi not needed for tree viewing
        false, // not a property
        null, // no parent
      );

      // Promote the group after successful operation
      await objectGroupManager.promoteNext();

      return RPCResponse.successMap({
        'root': rootNode.json,
        'groupName': group.groupName,
      });
    } catch (e, stack) {
      // Cancel the group on error
      await objectGroupManager.cancelNext();
      return RPCResponse.error('Error getting diagnostic tree: $e', stack);
    }
  } catch (e, stack) {
    return RPCResponse.error('Error creating object group: $e', stack);
  }
}