Add a delay before displaying loading indicator
Learn how to add a delay before displaying a loading indicator in Flutter to improve user experience by avoiding flicker effects during fast operations.
When your app performs fast operations, showing a progress loader for a split second can create a jarring “flicker” effect. Users notice this and it can make your app feel unpolished.
Solution
Add a minimum display delay to your loader. This ensures the loader only appears if the operation takes longer than expected. Have a look at the screenshot to understand it better.
Timer? _loaderTimer;
bool _isLoading = false;
Future<void> _fetchData() async {
// Schedule the loader to appear after 500ms
_loaderTimer = Timer(const Duration(milliseconds: 500), () {
if (!_isLoading) setState(() => _isLoading = true);
});
try {
await yourAsyncOperation();
} finally {
// Cancel the timer (if it hasn’t triggered yet)
_loaderTimer?.cancel();
setState(() => _isLoading = false);
}
}
Why This example works:
- No flicker: If the task finishes before 500ms, the loader never appears.
- Smooth UX: For longer tasks, the loader shows after the delay, avoiding abrupt transitions