flutter - 在测试期间我应该怎么做“没有找到方向性小部件”
问题描述
我正在尝试通过以下方式测试包含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 放入我的测试代码中时,这在我看来就像是很多额外的代码。有没有办法避免输入大量额外的测试代码来处理方向性?
解决方案
推荐阅读
- perl - 鉴于 CGI.pm 的弃用状态,我应该从 CGI::Fast 迁移到其他东西吗?我应该怎么做?
- google-cloud-dataflow - 作业卡在“未开始”状态
- http - URL 中的特殊字符“@”
- oracle - 锁定文件的选择语句
- kotlin - 在 Kotlin 中使用多变量的 For 循环
- android - 与 android 支持库的依赖关系不适用于使用 Androidx 的项目
- powershell - 使用脚本powershell通过IP地址自动重命名并加入csv文件中的多台计算机
- c# - 如何在 addParameter 中插入 asc/desc 以在 SQL 查询中进行排序
- ruby-on-rails - 如何从循环中在 ruby 中创建 JSON 对象的 JSON 数组
- c# - 多重性在关系“CW_FirmaCommunication_CwFirma”中的角色“CW_FirmaCommunication_CwFirma_Source”中无效