diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index 4bc574f6d5..a877dcea85 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -2860,7 +2860,7 @@ Iterable _describeRelevantUserCode(Element element) { bool processElement(Element target) { // TODO(chunhtai): should print out all the widgets that are about to cross // package boundaries. - if (_isLocalCreationLocation(target)) { + if (debugIsLocalCreationLocation(target)) { nodes.add( DiagnosticsBlock( name: 'The relevant error-causing widget was', @@ -2881,15 +2881,22 @@ Iterable _describeRelevantUserCode(Element element) { /// Returns if an object is user created. /// +/// This always returns false if it is not called in debug mode. +/// /// {@macro widgets.inspector.trackCreation} /// /// Currently is local creation locations are only available for /// [Widget] and [Element]. -bool _isLocalCreationLocation(Object object) { - final _Location location = _getCreationLocation(object); - if (location == null) - return false; - return WidgetInspectorService.instance._isLocalCreationLocation(location); +bool debugIsLocalCreationLocation(Object object) { + bool isLocal = false; + assert(() { + final _Location location = _getCreationLocation(object); + if (location == null) + isLocal = false; + isLocal = WidgetInspectorService.instance._isLocalCreationLocation(location); + return true; + }()); + return isLocal; } /// Returns the creation location of an object in String format if one is available. diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index 1d652b2834..6f13f8f710 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -2748,6 +2748,35 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { ); expect(node.toJsonMap(emptyDelegate), node.toJsonMap(defaultDelegate)); }); + + testWidgets('debugIsLocalCreationLocation test', (WidgetTester tester) async { + + final GlobalKey key = GlobalKey(); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Container( + padding: const EdgeInsets.all(8), + child: Text('target', key: key, textDirection: TextDirection.ltr), + ), + ), + ); + + final Element element = key.currentContext as Element; + + expect(debugIsLocalCreationLocation(element), isTrue); + expect(debugIsLocalCreationLocation(element.widget), isTrue); + + // Padding is inside container + final Finder paddingFinder = find.byType(Padding); + + final Element paddingElement = paddingFinder.evaluate().first; + + expect(debugIsLocalCreationLocation(paddingElement), isFalse); + expect(debugIsLocalCreationLocation(paddingElement.widget), isFalse); + }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // Test requires --track-widget-creation flag. + } }