diff --git a/packages/flutter/lib/src/foundation/diagnostics.dart b/packages/flutter/lib/src/foundation/diagnostics.dart index c09bc21dda..6a87e504f8 100644 --- a/packages/flutter/lib/src/foundation/diagnostics.dart +++ b/packages/flutter/lib/src/foundation/diagnostics.dart @@ -845,14 +845,10 @@ abstract class DiagnosticsNode { for (int i = 0; i < children.length; i++) { final DiagnosticsNode child = children[i]; - + assert(child != null); final TextTreeConfiguration childConfig = _childTextConfiguration(child, config); if (i == children.length - 1) { final String lastChildPrefixLineOne = '$prefixChildren${childConfig.prefixLastChildLineOne}'; - if (child == null) { - builder.writeRawLine('$lastChildPrefixLineOne'); - continue; - } builder.writeRawLine(child.toStringDeep( lastChildPrefixLineOne, '$prefixChildren${childConfig.childLinkSpace}${childConfig.prefixOtherLines}', @@ -862,14 +858,8 @@ abstract class DiagnosticsNode { builder.writeRaw('$prefixChildren${childConfig.childLinkSpace}${childConfig.footer}'); } else { final TextTreeConfiguration nextChildStyle = _childTextConfiguration(children[i + 1], config); - final String childPrefixLineOne = '$prefixChildren${childConfig.prefixLineOne}'; final String childPrefixOtherLines ='$prefixChildren${nextChildStyle.linkCharacter}${childConfig.prefixOtherLines}'; - - if (child == null) { - builder.writeRawLine('$childPrefixLineOne'); - continue; - } builder.writeRawLine(child.toStringDeep(childPrefixLineOne, childPrefixOtherLines)); if (childConfig.footer.isNotEmpty) builder.writeRaw('$prefixChildren${nextChildStyle.linkCharacter}${childConfig.footer}'); @@ -2139,13 +2129,18 @@ abstract class DiagnosticableTree extends Diagnosticable { /// Returns a list of [DiagnosticsNode] objects describing this node's /// children. /// - /// Children that are offstage should added with `style` + /// Children that are offstage should be added with `style` set to /// [DiagnosticsTreeStyle.offstage] to indicate that they are offstage. /// + /// The list must not contain any null entries. If there are explicit null + /// children to report, consider [new DiagnosticsNode.message] or + /// [DiagnosticsProperty] as possible [DiagnosticsNode] objects to + /// provide. + /// /// See also: /// /// * [RenderTable.debugDescribeChildren], which provides high quality custom - /// descriptions for child nodes. + /// descriptions for its child nodes. /// /// Used by [toStringDeep], [toDiagnosticsNode] and [toStringShallow]. @protected diff --git a/packages/flutter/lib/src/painting/text_span.dart b/packages/flutter/lib/src/painting/text_span.dart index 1e06fda557..1360576334 100644 --- a/packages/flutter/lib/src/painting/text_span.dart +++ b/packages/flutter/lib/src/painting/text_span.dart @@ -353,8 +353,14 @@ class TextSpan extends DiagnosticableTree { @override List debugDescribeChildren() { - return children == null ? - const [] : - children.map((TextSpan child) => child?.toDiagnosticsNode()).toList(); + if (children == null) + return const []; + return children.map((TextSpan child) { + if (child != null) { + return child.toDiagnosticsNode(); + } else { + return new DiagnosticsNode.message(''); + } + }).toList(); } } diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 49a5213328..4f9ace6fda 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -3326,7 +3326,11 @@ abstract class Element extends DiagnosticableTree implements BuildContext { List debugDescribeChildren() { final List children = []; visitChildren((Element child) { - children.add(child.toDiagnosticsNode()); + if (child != null) { + children.add(child.toDiagnosticsNode()); + } else { + children.add(new DiagnosticsNode.message('')); + } }); return children; } diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index 0099976a64..82d1eb3b54 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -101,6 +101,7 @@ class _WidgetInspectorState extends State final List children = object.debugDescribeChildren(); for (int i = children.length - 1; i >= 0; i -= 1) { final DiagnosticsNode diagnostics = children[i]; + assert(diagnostics != null); if (diagnostics.style == DiagnosticsTreeStyle.offstage || diagnostics.value is! RenderObject) continue; diff --git a/packages/flutter/test/painting/text_span_test.dart b/packages/flutter/test/painting/text_span_test.dart index f2dffb12f9..001219238a 100644 --- a/packages/flutter/test/painting/text_span_test.dart +++ b/packages/flutter/test/painting/text_span_test.dart @@ -30,24 +30,24 @@ void main() { expect(c1 == b2, isFalse); }); - test('TextSpan', () { + test('TextSpan toStringDeep', () { final TextSpan test = const TextSpan( text: 'a', style: const TextStyle( - fontSize: 10.0 + fontSize: 10.0, ), children: const [ const TextSpan( text: 'b', children: const [ - const TextSpan() - ] + const TextSpan(), + ], ), null, const TextSpan( - text: 'c' + text: 'c', ), - ] + ], ); expect(test.toStringDeep(), equals( 'TextSpan:\n' @@ -58,7 +58,7 @@ void main() { ' "b"\n' ' TextSpan:\n' ' (empty)\n' - ' \n' + ' \n' ' TextSpan:\n' ' "c"\n' )); diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index bfa0119cc8..76ccd94b87 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -506,4 +506,42 @@ void main() { ), ); }); + + testWidgets('Element diagnostics with null child', (WidgetTester tester) async { + await tester.pumpWidget(new NullChildTest()); + final NullChildElement test = tester.element(find.byType(NullChildTest)); + test.includeChild = true; + expect( + tester.binding.renderViewElement.toStringDeep(), + equalsIgnoringHashCodes( + '[root](renderObject: RenderView#4a0f0)\n' + 'â””NullChildTest(dirty)\n' + ' â””\n', + ), + ); + test.includeChild = false; + }); } + +class NullChildTest extends Widget { + @override + Element createElement() => new NullChildElement(this); +} + +class NullChildElement extends Element { + NullChildElement(Widget widget) : super(widget); + + bool includeChild = false; + + @override + void visitChildren(ElementVisitor visitor) { + if (includeChild) + visitor(null); + } + + @override + void forgetChild(Element child) { } + + @override + void performRebuild() { } +} \ No newline at end of file