Tillitsdone
down Scroll to discover

Using Dio Interceptors for Logging in Flutter

Learn how to implement Dio interceptors in Flutter for logging and modifying HTTP requests.

Master request handling with practical examples and best practices for API integration.
thumbnail

A futuristic network of glowing cyan and white light streams flowing through abstract black geometric shapes captured from a top-down perspective with dynamic motion blur high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Using Dio Interceptors for Logging and Modifying Requests in Flutter

Ever found yourself debugging API calls in Flutter and wishing you had better visibility into what’s happening under the hood? That’s where Dio interceptors come in – they’re like your personal network traffic controllers, letting you peek into and modify HTTP requests and responses with ease.

Abstract floating cloud formations in bright emerald green and charcoal black with streaming rays of sunlight cutting through shot from a low angle perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Getting Started with Dio Interceptors

Before diving deep, make sure you have Dio in your pubspec.yaml:

dependencies:
dio: ^5.0.0

Let’s start with a simple logging interceptor. This is super helpful when you’re trying to debug your API calls:

class LoggingInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
print('REQUEST[${options.method}] => PATH: ${options.path}');
return super.onRequest(options, handler);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
print('RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path}');
return super.onResponse(response, handler);
}
@override
void onError(DioError err, ErrorInterceptorHandler handler) {
print('ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path}');
return super.onError(err, handler);
}
}

Advanced Request Modification

Sometimes you need to modify every request automatically – like adding authentication tokens. Here’s how you can do that:

class AuthInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// Add auth token to headers
options.headers['Authorization'] = 'Bearer your-token-here';
return super.onRequest(options, handler);
}
}

Minimalist terrain landscape with rolling hills in contemporary brown and cream tones aerial perspective showing natural flowing patterns high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Putting It All Together

Now, let’s see how to use these interceptors in your Dio instance:

final dio = Dio();
dio.interceptors.add(LoggingInterceptor());
dio.interceptors.add(AuthInterceptor());

You can even chain multiple interceptors – they’ll execute in the order you add them. This is particularly useful when you want to log the modified request after your auth interceptor has done its job.

Best Practices

  1. Keep interceptors focused on a single responsibility
  2. Use interceptors for cross-cutting concerns like logging and authentication
  3. Remember that interceptors run for every request, so keep them lightweight
  4. Consider creating different Dio instances for different use cases

Modern abstract geometric shapes in slate grey and bright turquoise floating in space captured from a diagonal three-quarter view angle high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svgicons/flutter.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.