Sub topic 9
Testing Async UI and Timers
This chapter assumes you know Dart async and await, StatefulWidget, and how to run flutter test. Widget tests use fake time, so you can test delayed work without making the test wait in real time.
Why fake time matters
Async UI often has several states:
- Loading.
- Loaded successfully.
- Failed.
- Updated after a timer.
A widget test starts with one frame. Calling pump rebuilds the widget and can move fake time forward. Calling pump with a Duration advances the test clock by that duration. pumpAndSettle repeatedly pumps until no more frames are scheduled.
Testing a delayed Future
Inject the asynchronous operation into the widget. The test can then provide a fake implementation instead of calling a network service.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class ProfileName extends StatefulWidget {
const ProfileName({super.key, required this.loadName});
final Future<String> Function() loadName;
@override
State<ProfileName> createState() => _ProfileNameState();
}
class _ProfileNameState extends State<ProfileName> {
late final Future<String> _name = widget.loadName();
@override
Widget build(BuildContext context) {
return FutureBuilder<String>(
future: _name,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
return Text('Hello, ${snapshot.data}');
},
);
}
}
void main() {
testWidgets('shows loading, then the profile name', (tester) async {
Future<String> fakeLoadName() async {
await Future<void>.delayed(const Duration(seconds: 1));
return 'Ada';
}
await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: ProfileName(loadName: fakeLoadName)),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle();
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.text('Hello, Ada'), findsOneWidget);
});
}The first assertion checks the initial loading state. pumpAndSettle advances fake time until the delayed Future completes and the widget rebuilds.
Choosing how to pump
| Method | Use it when | Caution |
|---|---|---|
| `pump()` | One state change should happen immediately | It does not advance a meaningful delay |
| `pump(duration)` | Testing a known timer or animation interval | You must choose the relevant duration |
| `pumpAndSettle()` | The UI should finish all short async work | It can time out with repeating timers or endless animations |
For a Timer, use a duration that reaches the timer:
await tester.pump(const Duration(seconds: 1));Do not use real sleeps such as Future.delayed in the test itself. They slow the suite and make timing less predictable. Put delays in the fake dependency, then advance time with pump.
Common failures
If the loading widget remains visible, check that the Future was created and that the test pumped after its completion. If pumpAndSettle times out, search for a repeating timer, an active animation, or a widget that schedules frames continuously. If a test passes alone but fails in a group, reset shared fakes and dispose controllers or timers in the widget's dispose method.
Exercise
Which pump call is appropriate for testing a one-second Timer?
Show answer
Use await tester.pump(const Duration(seconds: 1)); then assert the UI after the timer's callback has run.