In Flutter development, we often trigger multiple Future-based tasks — from API calls, database
writes, to Cubit events — but we don’t always have a clean way to know exactly when all of them are
done.
Enter Completer, Dart’s most powerful tool for manual control over async flow.
| ✅ Use Case | 📌 Description |
|---|---|
| Wait For Cubit/BLoC event to finish | Show toast, move to next screen |
| Wait for multiple async functions | Download district, ward, vdc in parallel |
| Coordinate after isolate/task completes | Background parsing or long file operations |
| Show loader until all actions done | Prevent double tap, ensure clean UX |
| Bridge non-Future APIs | Turn callbacks into awaitable logic |
| Trigger UI actions post-completion | Show dialog, navigate, unlock interaction |
void download({required Completer<void> completer}) async {
try {
await fetchFromApi();
await saveToLocalDB();
completer.complete();
} catch (e) {
completer.completeError(e);
}
}
final cubit1 = Completer<void>();
final cubit2 = Completer<void>();
final cubit3 = Completer<void>();
context.read<Cubit1>().download(completer: cubit1);
context.read<Cubit2>().download(completer: cubit2);
context.read<Cubit3>().download(completer: cubit3);
Future.wait([
cubit1.future,
cubit2.future,
cubit3.future,
]).then((_) {
showDialog(context: context, builder: (_) => SuccessDialog());
}).catchError((e) {
showToast("❌ Failed: $e");
});
Future<void> waitForAll(List<Completer<void>> completers) {
return Future.wait(completers.map((c) => c.future));
}
final c1 = Completer<void>(), c2 = Completer<void>();
myService1.download(completer: c1);
myService2.upload(completer: c2);
await waitForAll([c1, c2]);
Think of Completer as your async completion notifier.
It doesn’t replace state management — it completes it.
Whether you're building enterprise-level apps, coordinating offline sync, or waiting for a background isolate, Completer gives you the full control you need.
#FlutterDev #MobileApps #Completer #DartTips #CleanArchitecture #AsyncProgramming #ProductivityHacks #FlutterTips