Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

Flutter: How to get properly RichText data when testing?

I'm currently writing a test in order to verify if a value inside a RichText is valid or not.

Here is the widget I need to test:

RichText(
  key: ValueKey('RichTextKey'),
  text: TextSpan(
    text: 'Part 1 - ',
    children: [
      TextSpan(text: 'Part 2'),
    ],
  ),
);

So currently I found a way to do it but it seems to be deprecated :/

Here is how I test for now (deprecated):

final richText0Finder = find.byKey(
  ValueKey('RichTextKey'),
);

final richText0Widget = tester.element(richText0Finder).widget as RichText;

// second ".text" is deprecated here :/
expect(richText0Widget.text.text, 'Part 1 - ');

// no problem with TextSpan inside RichText, the first element is not the one inside RichText...
final textSpan0 = richText0Widget.text.children.last as TextSpan;
expect(textSpan0.text, 'Part 2');

Deprecated say:

'text' is deprecated and shouldn't be used. InlineSpan does not innately have text. Use TextSpan.text instead. This feature was deprecated after v1.7.3..
Try replacing the use of the deprecated member with the replacement.

But I don't know how to get InlineSpan data inside a RichText widget ?

Any help would be great :) , Thanks !

question from:https://stackoverflow.com/questions/65885970/flutter-how-to-get-properly-richtext-data-when-testing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So I found the solution, just need to wrap richText0Widget.text as a TextSpan widget :

expect((richText0Widget.text as TextSpan).text, 'Part 1 - ');

Hope it can well :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...