One of the first things that an app developer is going to consider is how to make network requests. Dart has a builtin HTTP package, however, there exists a more powerful package called Dio. Dio provides a robust and easy-to-use API for making HTTP requests, handling interceptors, and dealing with complex scenarios like file uploads and downloads.
So why Dio?
Just like how
Axios is preferred over
- Interceptors: Dio allows you to intercept requests and responses, enabling you to log traffic, add authentication headers, or modify the request body before it's sent to the server.
- Multipart Form Data: Whereas you can send multipart form data with the default HTTP package, I feel like Dio simplifies this considerably (see here).
- Error Handling: Dio provides a comprehensive error handling mechanism, allowing you to gracefully handle network errors and provide meaningful feedback to users. That said, I do use Riverpod for state management, which also has similar capabilities.
- Global Configuration: Dio allows you to set global configuration options for all requests, such as base URLs, timeouts, and request headers.
Making HTTP Requests with Dio
You can of course use the other HTTP verbs to make requests.
Interceptors provide a powerful way to intercept and modify requests and responses. Dio allows you to add multiple interceptors, each with its own logic.
This code snippet logs the request URL to the console before sending the request.
A more realistic use case is adding authorisation to all network requests
Mocking Dio Requests for Testing
You're going to want to test code that makes network requests, but of course, you should not be contacting external APIs in tests. I like doing this using
http_mock_adapter and the
DioInterceptor it provides.