Sub topic 6
Rebuilding the Tree: `pump` Versus `pumpAndSettle`
This chapter assumes that you know basic Dart, Flutter widgets, testWidgets, finders, and matchers. It also assumes that you can run flutter test.
A widget test does not automatically redraw the screen after every action. WidgetTester.pump tells Flutter to process a frame. Choosing the right pump method makes tests predictable.
What `pump` does
pump() runs one frame. Use it after an interaction when you want Flutter to process a synchronous setState call.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('1'), findsOneWidget);You can pass a duration to pump. Flutter advances its test clock by that duration and processes a frame. This is useful for animations, timers, and delayed futures.
await tester.pump(const Duration(milliseconds: 500));The duration does not make the test wait in real time. The test remains fast because Flutter uses a fake clock.
What `pumpAndSettle` does
pumpAndSettle() repeatedly pumps frames until Flutter has no more scheduled frames. It is useful when a finite animation or a short asynchronous operation must finish before you check the result.
The method returns the number of frames it pumped. Most tests do not need that return value.
| Method | Main use | Common risk |
|---|---|---|
| `pump()` | Process one frame after an action | The animation or future may not be complete |
| `pump(duration)` | Move fake time forward | The duration may be too short |
| `pumpAndSettle()` | Wait for finite activity to finish | It can time out on repeating animations |
Choosing the right method
A button that changes text synchronously usually needs one pump():
await tester.tap(find.byKey(const Key('submit')));
await tester.pump();A delayed operation needs a duration that covers the delay:
await tester.pump(const Duration(seconds: 1));A finite transition can use pumpAndSettle():
await tester.tap(find.byKey(const Key('show-details')));
await tester.pumpAndSettle();Do not use pumpAndSettle() with a continuously repeating progress indicator, an infinite animation, or a widget that continually schedules frames. The test can time out because the tree never becomes idle.
Worked example with a delayed future
The following widget displays a spinner until its future completes.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class DelayedGreeting extends StatelessWidget {
const DelayedGreeting({super.key, required this.loadName});
final Future<String> Function() loadName;
@override
Widget build(BuildContext context) {
return FutureBuilder<String>(
future: loadName(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
return Text('Hello, ${snapshot.data}');
},
);
}
}
void main() {
testWidgets('shows the greeting after one second', (tester) async {
Future<String> loadName() async {
await Future<void>.delayed(const Duration(seconds: 1));
return 'Ada';
}
await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: DelayedGreeting(loadName: loadName)),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pump(const Duration(seconds: 1));
expect(find.text('Hello, Ada'), findsOneWidget);
});
}The first pumpWidget builds the initial loading state. The second pump advances fake time by one second, allowing the future to complete and the widget to rebuild.
Exercise
A button starts a two-second animation. Which call should you use to inspect the halfway state?
Show answer
Use pump with a Duration of 1 second, such as await tester.pump(const Duration(seconds: 1)).