On this page
article
Defer
Defer schedules the wrapped function to run in the next microtask. The returned future begins execution as soon as possible without blocking the current synchronous block.
Defer
What it is
Defer schedules the wrapped function to run in the next microtask. The returned future begins execution as soon as possible without blocking the current synchronous block.
When to use it
- Moving work out of a build or layout phase in Flutter.
- Batching multiple synchronous operations so they all run after the current event loop iteration.
- Creating a “lazy promise” that starts work immediately but allows awaiting later.
Async / sync support
| Wrapper | Support |
|---|---|
Func<R> | ✅ Async |
Func1<T, R> | ✅ Async |
Func2<T1, T2, R> | ✅ Async |
FuncSync<R> | ❌ No |
API reference
| |
No parameters.
Examples
Basic example
| |
Real-world example
| |
Best practices
- Use
deferwhen you want to start work as soon as the current microtask completes. - Combine with
awaitwhen you need the result before continuing. - Do not rely on defer for precise timing; it only guarantees the next microtask, not a specific delay.
Common pitfalls
- Unawaited fire-and-forget: If you never await the returned future, exceptions become unhandled asynchronous errors.
- Confusing with
delay:delaywaits a specific duration;deferonly yields to the event loop.