mercredi 12 septembre 2018

How to find the `text` property of a Widget during testing in Flutter?

I have a piece of code that creates a Table of Text widgets, like this:

return Table(
  defaultColumnWidth: FixedColumnWidth(120.0),
  children: <TableRow>[
    TableRow(
      children: <Widget>[Text('toffee'), Text('potato')],
    ),
    TableRow(
      children: <Widget>[Text('cheese'), Text('pie')],
    ),
  ],
);

I want to test that the first item in the Table is indeed the word 'toffee'. I set up my test and get to this part:

var firstCell = find
      .descendant(
        of: find.byType(Table),
        matching: find.byType(Text),
      )
      .evaluate()
      .toList()[0].widget;

  expect(firstCell, 'toffee');

This definitely doesn't work because firstCell is of type Widget, which is not equal to the String toffee.

I only see a toString() function, like this:

'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
 textAlign: left)'

How do I extract the text property to get the word toffee?

Right now it seems like all I can do is check that the .toString().contains('toffee') which isn't ideal.

Aucun commentaire:

Enregistrer un commentaire