NetworkingEasy20 XP3 min read
What is the difference between the http package and Dio in Flutter?
TL;DR: The http package is a simple, low-level HTTP client. Dio is a feature-rich client with interceptors, automatic JSON decoding, request cancellation, timeouts, file upload progress, and retry support built-in.
Full Answer
| Aspect | http package | Dio |
|---|---|---|
| Complexity | Low โ basic GET/POST | High โ full-featured |
| Interceptors | No | Yes โ auth, logging, retry |
| Cancellation | No | CancelToken |
| JSON decode | Manual jsonDecode() | Automatic with transformer |
| File upload | Manual | FormData + progress callback |
| Testing | Mock with http.Client | Mock with DioMixin or Dio adapter |
๐ฏ
For simple apps with a few API calls, http is fine. For anything with auth tokens, refresh logic, or error handling, use Dio with interceptors.
Code Examples
dartDio setup with auth interceptor
Output
// Every request gets auth header automatically // 401 triggers token refresh and retries the request
Common Mistakes
- โCreating a new Dio instance per request โ Dio should be a singleton with shared interceptors
- โNot handling DioException types (connection timeout, receive timeout, cancel) separately
Interview Tip
๐ก
Show the auth interceptor pattern with token refresh. It's the most common real-world Dio pattern and demonstrates understanding of interceptor ordering.
#dio#http#interceptor#retrofit