测试中未找到材质小部件

问题描述 投票:0回答:2

当我运行以下测试时,我收到

No material widget found
错误,即使我将其包装在 MaterialApp 中。有任何指示说明这是为什么吗?

void main() {
  final mockExam = examMocks[0];
  Widget createGridItem() => Provider<ExamInformation>.value(
        value: ExamInformation(
            examSpecifications: mockExam.examSpecifications,
            examImage: mockExam.examImage,
            questionList: mockExam.questionList),
        child: ExamGridItem(),
      );

  testWidgets('Exam grid item displays Footer', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(home: createGridItem()));

    final gridItemFooterFinder = find.byType(Footer);
    expect(gridItemFooterFinder, findsOneWidget);
  });
}

详细错误日志:

The following assertion was thrown building _InkResponseStateWidget(gestures: [tap], mouseCursor:
null, BoxShape.circle, dirty, state: _InkResponseState#3ef96):
No Material widget found.
_InkResponseStateWidget widgets require a Material widget ancestor.
In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
material library, that material is represented by the Material widget. It is the Material widget
that renders ink splashes, for instance. Because of this, many material library widgets require that
there be a Material widget in the tree above them.
To introduce a Material widget, you can either directly include one, or use a widget that contains
Material itself, such as a Card, Dialog, Drawer, or Scaffold.

The specific widget that could not find a Material ancestor was:
  _InkResponseStateWidget
The ancestors of this widget were:
  ...
  InkResponse
  Positioned
  Stack
  GridTile
  Consumer<ExamInformation>
  ...
flutter testing
2个回答
1
投票

你必须将你的小部件放入 Material 小部件中。

示例:

Material(child: InkWell())

导致此错误的小部件显示在错误描述中:

  InkResponse
  Positioned
  Stack
  GridTile
  Consumer<ExamInformation>

0
投票

在编写小部件测试时遇到同样的问题。

错误说明了一切。

The following assertion was thrown building ListTile(title: Text, trailing: CustomIcon, onTap:
  Closure: () => void, selectedTileColor: Color(0xfff3f3f3), dirty):
  No Material widget found.
  ListTile widgets require a Material widget ancestor within the closest LookupBoundary.

我的小部件是简单的小部件包装

ListTile
,正如您所看到的错误所示

ListTile widgets require a Material widget ancestor within the closest LookupBoundary

Widget 在真实页面上使用时工作得非常好,因为该页面是围绕

Scaffold
构建的,它为 ListTile 提供了 Material 小部件祖先,ListTile 是我的自定义小部件的一部分。

错误中也解释了这一点 ListTile 小部件需要最近 LookupBoundary 内的 Material 小部件祖先。

  In Material Design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
  material library, that material is represented by the Material widget. It is the Material widget
  that renders ink splashes, for instance. Because of this, many material library widgets require that
  there be a Material widget in the tree above them.
  To introduce a Material widget, you can either directly include one, or use a widget that contains
  Material itself, such as a Card, Dialog, Drawer, or Scaffold.

解决方案

代码之前

await $.pumpWidgetAndSettle(
      wrapWithApp(
        child: widgetUnderTest)

代码后

await $.pumpWidgetAndSettle(
      wrapWithApp(
        child: Material(child: widgetUnderTest))

所以基本上将我的 widgetUnderTest 包装在 Material 中。

参考资料未找到材质小部件

© www.soinside.com 2019 - 2024. All rights reserved.