首页 > 解决方案 > 在测试期间我应该怎么做“没有找到方向性小部件”

问题描述

我正在尝试通过以下方式测试包含MarkdownBody的小部件:

testWidgets('Answer displayed correctly',
    (WidgetTester tester) async {
  await tester.pumpWidget(MyWidget("text")));
...

我收到错误:

The following assertion was thrown building MarkdownBody(state: _MarkdownWidgetState#9550f):
No Directionality widget found.
RichText widgets require a Directionality widget ancestor.
The specific widget that could not find a Directionality ancestor was:
  RichText
The ownership chain for the affected widget is: "RichText ← Wrap ← Column ← MarkdownBody ← Center ←
  Padding ← Expanded ← Row ← Column ← DecoratedBox ← ⋯"
Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
top of your application widget tree. It determines the ambient reading direction and is used, for
example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.

The relevant error-causing widget was:
  MarkdownBody

显然,这里出现这个错误是因为我直接测试了我的 Widget,而不是在 MaterialApp 或 WidgetsApp 中使用它。当我总是必须将 Directionality Widgets 放入我的测试代码中时,这在我看来就像是很多额外的代码。有没有办法避免输入大量额外的测试代码来处理方向性?

标签: flutterdartflutter-test

解决方案


推荐阅读