← → or space · progress saves for Continue on the roadmap
Goal
Show activity on the terminal while a Future runs, using Timer.periodic.
Step 1 - Spinner frames
import 'dart:async';
import 'dart:io';
Future<void> withSpinner(Future<void> work) async {
const frames = ['|', '/', '-', '\\'];
var i = 0;
final timer = Timer.periodic(const Duration(milliseconds: 120), (_) {
stdout.write('\r${frames[i % frames.length]} loading...');
i++;
});
try {
await work;
} finally {
timer.cancel();
stdout.write('\r');
stdout.writeln('done. ');
}
}
Future<void> slowWork() async {
await Future.delayed(const Duration(seconds: 2));
}
Future<void> main() async {
await withSpinner(slowWork());
}Step 2 - One-line progress
- Replace frames with a growing
.sequence or a numeric counter if you prefer minimal output.
Practice tasks
- Pass a
String messageintowithSpinnerand show it after the frame. - If
workthrows, print the error on a new line after clearing the spinner line. - Use
Future.anywith a timeout future to stop waiting and cancel the timer on timeout.