[英]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'.我想测试表中的第一项确实是“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
.这绝对行不通,因为firstCell
是 Widget 类型,它不等于 String toffee
。
I only see a toString()
function, like this:我只看到一个toString()
函数,像这样:
'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
textAlign: left)'
How do I extract the text
property to get the word toffee
?如何提取text
属性以获取单词toffee
?
Right now it seems like all I can do is check that the .toString().contains('toffee')
which isn't ideal.现在看来我所能做的就是检查.toString().contains('toffee')
是否不理想。
You can cast your firstCell
to Text
.您可以将firstCell
为Text
。
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.whereType<Text>()
.first;
Then test firstCell.data
然后测试firstCell.data
expect(firstCell.data, 'toffee');
Rémi's example doesn't quite work - it may have worked at the time he answered, but at this time calling whereType<Text>()
will always return an empty Iterable
because evaluate()
returns Iterable<Element>
, not Iterable<Widget>
. Rémi 的例子不太有效 - 在他回答时它可能有效,但此时调用whereType<Text>()
将始终返回一个空的Iterable
因为evaluate()
返回Iterable<Element>
,而不是Iterable<Widget>
. You can however get an Element
's Widget by calling .widget
on it, so the following code should work:但是,您可以通过调用.widget
来获取Element
的 Widget,因此以下代码应该可以工作:
Text firstText = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.first
.widget;
expect(firstText.data, 'toffee');
The OP was very close to having working code - there were only 2 minor issues: OP 非常接近具有工作代码 - 只有 2 个小问题:
var
instead of Text
, the type of the variable was Widget
通过使用var
而不是Text
,变量的类型是Widget
Widget
was compared to a String
- this could never return true - the intent was to compare a property of the Widget
to the String
- in the case of a Text
, the String
it displays is obtained by calling .data
on the Text
将Widget
与String
进行比较 - 这永远不会返回 true - 目的是将Widget
的属性与String
进行比较 - 在Text
的情况下,它显示的String
是通过在Text
上调用.data
获得的Edit:编辑:
The WidgetTester
now has utility functions for retrieving widgets: widget(Finder)
, widgetList(Finder)
, firstWidget(Finder)
and allWidgets
. WidgetTester
现在具有用于检索小部件的实用函数: widget(Finder)
、 widgetList(Finder)
、 firstWidget(Finder)
和allWidgets
。 So for the OP's use case you would be using firstWidget
like so:因此,对于 OP 的用例,您将像这样使用firstWidget
:
Text firstText = tester.firstWidget(
find.descendant(
of: find.byType(Table),
matching: find.byType(Text),
));
expect(firstText.data, 'toffee');
按文字查找?
expect(find.text('toffee'), findsOneWidget);
You can use short form for any type of widgets:您可以对任何类型的小部件使用简写形式:
expect(
(find.byType(InAppWebView).evaluate().single.widget as InAppWebView)
.initialUrl,
'https://www.google.com/');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.