pumpWidget method Null safety

Future<void> pumpWidget(
  1. Widget widget,
  2. [Duration? duration,
  3. EnginePhase phase = EnginePhase.sendSemanticsUpdate]
)

Renders the UI from the given widget.

Calls runApp with the given widget, then triggers a frame and flushes microtasks, by calling pump with the same duration (if any). The supplied EnginePhase is the final phase reached during the pump pass; if not supplied, the whole pass is executed.

Subsequent calls to this is different from pump in that it forces a full rebuild of the tree, even if widget is the same as the previous call. pump will only rebuild the widgets that have changed.

This method should not be used as the first parameter to an expect or expectLater call to test that a widget throws an exception. Instead, use TestWidgetsFlutterBinding.takeException.

testWidgets('MyWidget asserts invalid bounds', (WidgetTester tester) async {
  await tester.pumpWidget(MyWidget(-1));
  expect(tester.takeException(), isAssertionError); // or isNull, as appropriate.
});

See also LiveTestWidgetsFlutterBindingFramePolicy, which affects how this method works when the test is run with flutter run.

Implementation

Future<void> pumpWidget(
  Widget widget, [
  Duration? duration,
  EnginePhase phase = EnginePhase.sendSemanticsUpdate,
]) {
  return TestAsyncUtils.guard<void>(() {
    binding.attachRootWidget(widget);
    binding.scheduleFrame();
    return binding.pump(duration, phase);
  });
}