【问题标题】:How to use find.byType matching of List in Flutter widget testingFlutter小部件测试中如何使用List的find.byType匹配
【发布时间】:2019-12-06 14:55:20
【问题描述】:

我想为 Stack 进行小部件测试。这是示例代码

final List<Widget> children = [];
final stack = Stack(children: children);
await tester.pumpWidget(Container(child: stack));
...

final stackFinder = find.byWidget(stack);
expect(stackFinder, findsOneWidget);

// children should be in Stack
final childrenFinder = find.descendant(
  of: stackFinder,
  matching: find.byType(children.runtimeType),
);
expect(childrenFinder, findsWidgets);

但得到错误:

Expected: at least one matching node in the widget tree
  Actual: ?:<zero widgets with type "List<Widget>" that has ancestor(s) with type "Stack"

我尝试将匹配更改为matching: find.byType([].runtimeType),matching: find.byType(List&lt;Widget&gt;),,但它也不起作用。

你知道怎么解决吗?或者,如果不可能,您使用哪种方法来验证里面的孩子的数量?

【问题讨论】:

    标签: testing flutter widget


    【解决方案1】:

    List&lt;Widgets&gt; 不是小部件。因此,您无法创建一个查找该类型的查找器。

    您可以改为查找 Widget 并检查您是否找到了您期望的正确数量的小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-31
      • 2019-02-25
      • 2022-06-24
      • 2021-05-09
      • 2021-05-14
      • 2022-12-12
      • 2020-03-21
      • 2019-06-11
      相关资源
      最近更新 更多