在测试中找不到材料小部件

时间:2020-10-28 21:08:20

标签: flutter testing

运行以下测试时,我收到了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>
  ...

1 个答案:

答案 0 :(得分:0)

您必须将您的窗口小部件放入“材料”窗口小部件中。

示例:

Material(child: InkWell())

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

  InkResponse
  Positioned
  Stack
  GridTile
  Consumer<ExamInformation>