Future.delayed() is one of the simplest and most useful ways in Dart to create a delay or simulate an asynchronous operation like an API call or background task. Basically, it allows you to run a piece of code after a specific duration, without blocking the rest of the program.
The syntax is:
Future.delayed(Duration duration, [FutureOr Function()? computation])
So you pass the time to wait and optionally a function that runs after the delay.
For example:
void main() {
print("Task 1 started");
Future.delayed(Duration(seconds: 2), () {
print("Task 2 executed after 2 seconds");
});
print("Task 3 started immediately");
}
When you run this, the output will be:
Task 1 started
Task 3 started immediately
Task 2 executed after 2 seconds
This clearly shows that Dart doesnβt block the thread β while waiting for the delay, other code keeps running.
In one of my Flutter projects, I used Future.delayed() to display a splash screen for a few seconds before navigating to the home screen. Something like this:
@override
void initState() {
super.initState();
Future.delayed(Duration(seconds: 3), () {
Navigator.pushReplacementNamed(context, '/home');
});
}
Here, the delay creates a smooth transition between screens without freezing the UI.
Another use case is simulating API calls while testing β instead of actually calling a backend, I can use Future.delayed() to mimic a network response time.
One challenge I faced was accidentally using it in tight loops, which can quickly queue up many delayed tasks β so I had to make sure delays were used thoughtfully, especially in periodic or timer-based logic.
If I need more control (like cancelling or repeating delays), I might use a Timer instead.
So overall, Future.delayed() is a neat way to simulate asynchronous behavior or introduce controlled waiting in Dart without blocking the main thread.
