If you’re building a Flutter app that communicates with APIs, handling HTTP requests efficiently is crucial. Among various options, the Dio package stands out as a powerful, flexible, and feature-rich HTTP client for Flutter.
In this 2025 guide, we’ll explore everything about the Dio package — its features, real-world use cases, and how to use it effectively in modern Flutter development.
What is Dio in Flutter?
Dio is a powerful HTTP client for Dart, used in Flutter apps to make network requests. It supports advanced configurations like interceptors, request cancellation, global configuration, form data, file uploading, custom timeouts, and more.
It is maintained actively and widely used in production-grade applications.
Key Features of Dio Package
Here’s what makes Dio one of the most popular Flutter networking packages in 2025:
1. Easy to Use API
With just a few lines, you can perform GET, POST, PUT, DELETE, and other HTTP methods.
2. Interceptors
Dio allows intercepting requests and responses for logging, authentication, error handling, or modifying headers.
3. Global Configuration
Set base URL, headers, timeouts, etc., once — and use it across the app.
4. Request Cancellation
You can cancel ongoing HTTP requests using CancelToken
, useful when navigating away from a screen or closing the app.
5. File Uploading/Downloading
Supports multipart/form-data
, progress tracking, and resume download capabilities.
6. Retry Mechanism
Support for retrying failed requests with exponential backoff.
7. FormData Support
Easily send complex forms or files in HTTP POST requests.
Installing Dio in Flutter
Add this to your pubspec.yaml
:
dependencies:
dio: ^5.4.0
Then run:
flutter pub get
Basic Example – GET Request
import 'package:dio/dio.dart';
void fetchData() async {
final dio = Dio();
final response = await dio.get('https://jsonplaceholder.typicode.com/posts');
print(response.data);
}
Example – POST Request with JSON Body
final dio = Dio();
void sendData() async {
final response = await dio.post(
'https://example.com/api/users',
data: {
"name": "John",
"email": "john@example.com",
},
);
print(response.data);
}
Using Interceptors in Dio
dio.interceptors.add(InterceptorsWrapper(
onRequest: (options, handler) {
print('Sending request to ${options.uri}');
return handler.next(options);
},
onResponse: (response, handler) {
print('Received response: ${response.statusCode}');
return handler.next(response);
},
onError: (DioException e, handler) {
print('Request error: ${e.message}');
return handler.next(e);
},
));
Canceling HTTP Requests in Dio
CancelToken cancelToken = CancelToken();
dio.get('https://api.example.com/data', cancelToken: cancelToken);
// Later in code
cancelToken.cancel("Request cancelled by user");
Uploading a File with Dio
FormData formData = FormData.fromMap({
"file": await MultipartFile.fromFile("path/to/file.jpg"),
});
final response = await dio.post(
"https://api.example.com/upload",
data: formData,
);
Real-World Use Cases of Dio in 2025
- Authentication Systems – Add headers for JWT tokens.
- Dynamic API Handling – Use interceptors to manage token refresh.
- Offline Support – Combine Dio with caching strategies.
- Progress Indicators – For uploading/downloading files.
- Retry Policies – For unstable networks using retry packages.
Best Practices
- Use a singleton for Dio instance with base options.
- Use interceptors for logging and error handling.
- Always wrap network code in try-catch blocks.
- Cancel unnecessary requests to save bandwidth.
Dio vs http Package – Quick Comparison
Feature | Dio | http |
---|---|---|
Interceptors | ✅ Yes | ❌ No |
Request Cancellation | ✅ Yes | ❌ No |
File Uploading | ✅ Yes | ⛔️ Limited |
Retry Support | ✅ Yes | ❌ No |
Global Config | ✅ Yes | ❌ No |
FormData | ✅ Yes | ❌ No |
Learning Curve | Medium | Easy |
Frequently Asked Questions (FAQs)
Dio is an advanced HTTP client used to make API calls, upload files, manage headers, and handle network-related tasks.
Yes, Dio offers more flexibility and advanced features like interceptors, request cancellation, and file handling which http
lacks.
Use CancelToken
and pass it while making the request. You can cancel it anytime using cancelToken.cancel()
.
Yes, Dio integrates well with all state management tools like Riverpod, GetX, or Bloc.
Dio is a must-have HTTP client for every serious Flutter developer in 2025. Its powerful features, flexibility, and active development make it an ideal choice for handling complex networking logic in mobile apps.
Whether you’re building a social app, an eCommerce platform, or a fintech app, Dio provides the reliability and control you need.