Assertion should allow `semanticsLabel` for TextSpan without `text`

This issue has been tracked since 2022-11-24.

I don't agree with the following assert:

assert(!(text == null && semanticsLabel != null));

Let's say I'm constructing a rich text with a root TextSpan consisting of a few TextSpans and (decorative - say - icons) WidgetSpans in its children. And I also want to provide universal semanticsLabel for the whole text, that gives better meaning to screen reader users.

Flutter 3.3.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision d9111f6402 (5 weeks ago) • 2022-10-19 12:27:13 -0700
Engine • revision 3ad69d7be3
Tools • Dart 2.18.2 • DevTools 2.15.0
exaby73 wrote this answer on 2022-11-25

Hello @Albert221. Help me understand your use case. The assertion says that you shouldn't have an empty TextSpan.text and also an empty semanticsLabel. The documentation for semanticsLabel states:

If present, the semantics of this span will contain this value instead of the actual text.
This is useful for replacing abbreviations or shorthands with the full text value:

Could you provide a code sample where the current behavior is undesirable and/or throws an assertion error during development?

Albert221 wrote this answer on 2022-11-25

Hi @exaby73, thanks for your response!

Actually, the assertion says that I cannot have semanticsLabel when I don't have text => I cannot have semanticsLabel when I have children.

Here's a quick example with failing assert:

import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: Scaffold(
          body: Text.rich(TextSpan(
            children: [
              TextSpan(
                children: const [
                  WidgetSpan(child: SizedBox()), // Some unread indicator
                  TextSpan(text: 'Notifications'),
                  TextSpan(text: '(1)'),
                ],
                semanticsLabel: '1 new notification',
              ),
              const TextSpan(text: 'Something else'),
            ],
          )),
        ),
      ),
    );
exaby73 wrote this answer on 2022-11-25

Okay I see your point here. I will label this issue for insights and visibility from the team. Thank you :)

More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-12-07
Star Count 147031
Watcher Count 3560
Fork Count 23915
Issue Count 11300

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date