Module 2
Topic 5

Error Handling

Handling network errors gracefully — from HTTP status codes to user-friendly error messages.

Flutter Docs – Error Handling
Why Error Handling Matters

When your app makes network requests, things can go wrong . The server might be down, the user might have no internet, or the request might timeout. Proper error handling is essential for:

  • User experience – Show friendly error messages instead of crashing
  • Debugging – Log errors for debugging and monitoring
  • Recovery – Allow users to retry failed operations
  • Reliability – Build apps that fail gracefully

⚠️ The Cost of Poor Error Handling

  • ❌ App crashes when a network request fails
  • ❌ Users see confusing error messages
  • ❌ No way to recover from errors
  • ❌ Hard to debug issues in production
HTTP Status Codes

Understanding HTTP status codes is crucial for proper error handling. Here are the most common ones:

200
Success
OK – Request succeeded
201
Success
Created – Resource created
204
Success
No Content – Deletion successful
400
Client Error
Bad Request – Invalid data
401
Client Error
Unauthorized – Need to log in
403
Client Error
Forbidden – No permission
404
Client Error
Not Found – Resource doesn't exist
429
Client Error
Too Many Requests – Rate limited
500
Server Error
Internal Server Error
502
Server Error
Bad Gateway
503
Server Error
Service Unavailable
504
Server Error
Gateway Timeout
Handling Status Codes in Flutter

Here's how to handle different status codes in your Flutter app with user-friendly messages.

Network Errors

Network errors happen when the device can't reach the server. Common causes include:

  • No internet connection
  • Server is down
  • Connection timeout
  • DNS resolution failure

⏱️ Timeouts

Always set timeouts for network requests. A request that hangs indefinitely is a poor user experience. Use timeout() on your Future to set a maximum wait time.

UI Error Handling

Here's how to display errors in your Flutter UI with a FutureBuilder .

Step-by-Step Explanation
1.
Define a custom exception – Create an ApiException class that holds a user-friendly message and optional status code.
2.
Handle status codes – Use a switch statement or conditionals to handle different HTTP status codes with appropriate messages.
3.
Catch network errors – Catch http.ClientException to handle network connectivity issues.
4.
Display errors in UI – Use FutureBuilder to show loading, error, and data states with user-friendly messages.
5.
Provide retry functionality – Add a "Try Again" button that re-fetches the data.
Error Handling Best Practices

✅ Do's

  • Always handle loading, error, and data states
  • Show user-friendly error messages (not technical jargon)
  • Provide a way to retry failed operations
  • Log errors for debugging and monitoring
  • Set timeouts for network requests
  • Check status codes before parsing responses

❌ Don'ts

  • Don't ignore errors or swallow exceptions
  • Don't show raw error messages (like stack traces) to users
  • Don't assume the network is always available
  • Don't parse responses without checking status codes
  • Don't forget to handle network timeouts
Complete Example: Error Handling Demo

Here's a complete app that demonstrates all error handling concepts with a simulated API.

Common Mistakes
❌ Mistake 1: Not checking status codes

Always check response.statusCode before parsing the response body. A 404 or 500 response may not contain the expected JSON structure.

✅ Correct: Always check status codes

Use if (response.statusCode == 200) before parsing, and throw exceptions for other status codes.

❌ Mistake 2: Showing raw error messages to users

Don't show snapshot.error.toString() directly. Users don't need to see stack traces.

✅ Correct: Show user-friendly messages

Map errors to user-friendly messages like "Network error. Please check your internet connection."

❌ Mistake 3: Not providing retry functionality

If a request fails, users should be able to retry without restarting the app.

✅ Correct: Always provide a retry button

Add a "Retry" button in the error state that re-fetches the data.

🎯 Key Takeaway

Error handling is essential for a good user experience. Always handle loading, error, and data states. Show user-friendly messages and provide a retry mechanism . Handle HTTP status codes properly and don't forget about network errors and timeouts. Users will forgive errors if they understand what went wrong and can recover.