diff --git a/packages/flutter/lib/src/painting/text_span.dart b/packages/flutter/lib/src/painting/text_span.dart index 46b752692d1..a0b5478191a 100644 --- a/packages/flutter/lib/src/painting/text_span.dart +++ b/packages/flutter/lib/src/painting/text_span.dart @@ -71,7 +71,8 @@ class TextSpan extends InlineSpan { TextStyle? style, this.recognizer, this.semanticsLabel, - }) : super(style: style); + }) : assert(!(text == null && semanticsLabel != null)), + super(style: style); /// The text contained in this span. /// @@ -279,7 +280,7 @@ class TextSpan extends InlineSpan { @override void computeSemanticsInformation(List collector) { assert(debugAssertIsValid()); - if (text != null || semanticsLabel != null) { + if (text != null) { collector.add(InlineSpanSemanticsInformation( text!, semanticsLabel: semanticsLabel, diff --git a/packages/flutter/test/painting/text_span_test.dart b/packages/flutter/test/painting/text_span_test.dart index 90b0dd9778d..cd4414feb55 100644 --- a/packages/flutter/test/painting/text_span_test.dart +++ b/packages/flutter/test/painting/text_span_test.dart @@ -231,4 +231,11 @@ void main() { expect(textSpan.getSpanForPosition(const TextPosition(offset: 2)).runtimeType, WidgetSpan); expect(textSpan.getSpanForPosition(const TextPosition(offset: 3)).runtimeType, TextSpan); }); + + test('TextSpan computeSemanticsInformation', () { + final List collector = []; + const TextSpan(text: 'aaa', semanticsLabel: 'bbb').computeSemanticsInformation(collector); + expect(collector[0].text, 'aaa'); + expect(collector[0].semanticsLabel, 'bbb'); + }); }