Loading and error states help keep your app responsive and clear. Showing a loading spinner or message uses minimal CPU and memory, so it won't slow your app down. However, if loading takes too long, users may think the app is frozen, hurting user experience. Efficient error handling prevents crashes and wasted resources by stopping failed operations early.
Loading and error states in Flutter - Build, Publish & Deploy
- Use lightweight widgets like
CircularProgressIndicatorfor loading spinners. - Load data asynchronously with
FutureBuilderorStreamBuilderto avoid blocking the UI thread. - Cache data when possible to reduce repeated loading.
- Show simple error messages with retry buttons instead of complex UI to keep rendering fast.
- Cancel unnecessary network requests if the user navigates away.
Loading and error states themselves add negligible size to your app bundle. Using Flutter's built-in widgets keeps your app small. Avoid adding large custom animations or images for these states to prevent increasing app size and startup time.
Both platforms support similar loading and error UI patterns in Flutter. However, iOS users expect smooth, native-style spinners and subtle error alerts, while Android users expect Material Design progress indicators and snackbars. Flutter's widgets adapt to platform style automatically if you use CupertinoActivityIndicator for iOS and CircularProgressIndicator for Android.
- Apple App Store: Ensure your app does not appear frozen during loading; use clear indicators. Avoid misleading error messages. Follow Human Interface Guidelines for alerts and spinners.
- Google Play Store: Provide meaningful error messages and retry options. Avoid crashes caused by unhandled errors. Follow Material Design guidelines for progress and error UI.
Your app takes 5 seconds to load this screen. What's likely wrong?
- Network requests are blocking the main thread instead of running asynchronously.
- No loading indicator is shown, so users think the app is frozen.
- Large data is loaded all at once instead of paginated or cached.
- Error states are not handled, causing retries or crashes.
Practice
Solution
Step 1: Understand loading state purpose
Loading states indicate to users that the app is busy fetching or processing data.Step 2: Differentiate from error state
Error states show problems, not progress. Loading states show progress.Final Answer:
To inform the user that data is being fetched or processed -> Option AQuick Check:
Loading state = show progress [OK]
- Confusing loading with error messages
- Thinking loading blocks app permanently
- Assuming loading triggers navigation
Solution
Step 1: Identify loading spinner widget
CircularProgressIndicator is the built-in widget for showing a spinner in Flutter.Step 2: Exclude other widgets
Text shows text, Container is a box, ListView shows lists, none show spinners.Final Answer:
CircularProgressIndicator -> Option CQuick Check:
Spinner widget = CircularProgressIndicator [OK]
- Using Text widget to show spinner
- Confusing Container with spinner
- Trying to use ListView for loading
bool isLoading = true;
Widget build(BuildContext context) {
if (isLoading) {
return CircularProgressIndicator();
} else {
return Text('Data loaded');
}
}Solution
Step 1: Check condition when isLoading is true
The if condition is true, so CircularProgressIndicator() is returned.Step 2: Understand widget output
CircularProgressIndicator shows a spinner, not text or error.Final Answer:
A circular loading spinner -> Option DQuick Check:
isLoading true shows spinner [OK]
- Assuming text shows when loading is true
- Expecting empty screen instead of spinner
- Thinking code causes error
bool hasError = false;
bool isLoading = true;
Widget build(BuildContext context) {
if (hasError) {
return Text('Error occurred');
} else if (isLoading) {
CircularProgressIndicator();
} else {
return Text('Content loaded');
}
}Solution
Step 1: Check all return statements
hasError branch returns Text, else branch returns Text, but isLoading branch calls CircularProgressIndicator without return.Step 2: Understand Flutter build method needs return
Every branch must return a widget; missing return causes error.Final Answer:
Missing return before CircularProgressIndicator() -> Option AQuick Check:
All branches must return widgets [OK]
- Forgetting return keyword before widgets
- Confusing variable names
- Thinking else if is wrong here
isLoading and hasError?Solution
Step 1: Prioritize error over loading
If there is an error, show error message first before loading or content.Step 2: Show loading only if no error and loading is true
Then show spinner if loading is true and no error.Step 3: Show content if no error and not loading
Finally, show content when no error and loading is false.Final Answer:
if (hasError) return Text('Error'); else if (isLoading) return CircularProgressIndicator(); else return Text('Content'); -> Option BQuick Check:
Error first, then loading, then content [OK]
- Showing loading before error
- Combining error and loading in one condition
- Using wrong widgets for states
Ready to prove it?
Pass the test and this pattern moves unknown
