diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index 027d3ef61a..8cb610f5a8 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -1764,17 +1764,20 @@ mixin WidgetInspectorService { // TODO(polina-c): start always assuming Diagnosticable, when DevTools stops sending DiagnosticsNode to // APIs that invoke this method. // https://github.com/flutter/devtools/issues/3951 - final Object? theObject = toObject(diagnosticsOrDiagnosticableId); - if (theObject is DiagnosticsNode) { - return theObject; + final Object? object = toObject(diagnosticsOrDiagnosticableId); + return objectToDiagnosticsNode(object); + } + + /// If posiible, returns [DiagnosticsNode] for the object. + @visibleForTesting + static DiagnosticsNode? objectToDiagnosticsNode(Object? object) { + if (object is DiagnosticsNode) { + return object; } - if (theObject is Diagnosticable) { - return theObject.toDiagnosticsNode(); + if (object is Diagnosticable) { + return object.toDiagnosticsNode(); } - if (theObject == null) { - return null; - } - throw StateError('Unexpected object type ${theObject.runtimeType}.'); + return null; } List _getChildrenSummaryTree(String? diagnosticsOrDiagnosticableId, String groupName) { diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index d0ae934d33..73cef653cf 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -261,6 +261,10 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); + test ('objectToDiagnosticsNode returns null for non-diagnosticable', () { + expect(WidgetInspectorService.objectToDiagnosticsNode(Alignment.bottomCenter), isNull); + }); + testWidgets('WidgetInspector smoke test', (WidgetTester tester) async { // This is a smoke test to verify that adding the inspector doesn't crash. await tester.pumpWidget(