Build HTTP client with Dio and interceptors
✓Works with OpenClaudeYou are a Flutter developer building production-ready HTTP clients. The user wants to set up Dio with interceptors for logging, error handling, and request/response transformation.
What to check first
- Run
flutter pub list-package-files dioto confirm Dio is installed - Check
pubspec.yamlto verifydio: ^5.3.0or later is declared - Ensure your Flutter project is on null safety (dart 2.12+)
Steps
- Add Dio to
pubspec.yaml:dio: ^5.3.0and runflutter pub get - Create a
lib/services/http_client.dartfile for your HTTP client singleton - Initialize Dio with
BaseOptionsincluding timeout, baseUrl, and headers - Create a custom
LoggingInterceptorextendingInterceptorto log requests and responses - Add
QueuedInterceptorsManagerto handle request queuing for token refresh scenarios - Create an
ErrorHandlingInterceptorto catch and transform Dio exceptions into app-specific errors - Register all interceptors using
dio.interceptors.add() - Implement request/response transformation in
onRequest()andonResponse()methods - Test with a sample API call to verify logging and error handling work end-to-end
Code
import 'package:dio/dio.dart';
class HttpClient {
static final HttpClient _instance = HttpClient._internal();
late Dio _dio;
factory HttpClient() {
return _instance;
}
HttpClient._internal() {
_dio = Dio(
BaseOptions(
baseUrl: 'https://api.example.com',
connectTimeout: const Duration(seconds: 30),
receiveTimeout: const Duration(seconds: 30),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
),
);
_dio.interceptors.add(_LoggingInterceptor());
_dio.interceptors.add(_ErrorHandlingInterceptor());
}
Future<T> get<T>(
String path, {
Map<String, dynamic>? queryParameters,
Options? options,
}) async {
try {
final response = await _dio.get<T>(
path,
queryParameters: queryParameters,
options: options,
);
return response.data as T;
} on DioException {
rethrow;
}
}
Future<T> post<T>(
String path, {
dynamic data,
Map<String, dynamic>? queryParameters,
Options? options,
}) async {
try {
final response = await _dio.post<T>(
path,
data: data,
queryParameters: queryParameters,
options:
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Flutter Skills
Other Claude Code skills in the same category — free to download.
Flutter Widget
Build custom Flutter widgets with state management
Flutter Riverpod
Set up Riverpod for state management in Flutter
Flutter Navigation
Configure GoRouter for declarative navigation
Flutter Firebase
Integrate Firebase (Auth, Firestore, Storage) in Flutter
Flutter Testing
Write widget tests and integration tests for Flutter
Flutter State Management with Riverpod
Manage app state in Flutter using Riverpod 2.x for type-safe reactive state
Flutter Platform Channels
Call native iOS and Android code from Flutter via platform channels
Want a Flutter skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.