我还没有找到用于测试 ListView 的复杂示例。示例。
我有一个ListView,它有三个对象
Person
。
Person{
String name;
String surname;
}
在 UI 中,
Person
被包裹在 Column
中,为它们提供了两个 Text
小部件,用于 name
和 surname
。
我点击FAB。单击 FAB 会添加新的
Person
,其中 name
Tom
和姓氏 Thomson
到 ListView
。现在Person
有4个对象。而且我知道最后一个的数据。
如何验证该项目已成功添加?有没有办法检查
length/count
或ListView
?
有没有办法验证最后添加的项目参数?
欣赏!
widgetList
在树中提供了匹配的小部件,只需提及需要测试的Listview
的键即可。
final count = tester
.widgetList<ListView>(find.byKey(ValueKey("ListViewKey")))
.length;
我使用唯一键添加我的 ListTile 标题(例如“tile_surname_#”)。假设您的 ListView 是使用名为 Persons 的 Person 对象数组构建的,您可以尝试以下操作:
final String valueKey = 'tile_$newPerson.surname_${persons.length}';
final newPersonFinder = find.byValueKey(valueKey);
expect(
await driver.getText(newPersonFinder), valueKey
);
final listItemsCount = tester.widgetList<ListView>(find.byType(ListView)).length;
expect(listItemsCount, equals(1));