lundi 25 janvier 2021

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 !

Aucun commentaire:

Enregistrer un commentaire