createDio function
Creates a new Dio instance with configured interceptors.
To debug with a self-signed certificate, pass --dart-define=ALLOW_BAD_CERTIFICATES=true to flutter run.
Cookies are shared across all clients via the global cookieJar.
Implementation
Dio createDio() {
final dio = Dio()
..options = BaseOptions(
validateStatus: (status) => status != null && status < 400,
followRedirects: false,
);
const allowBadCertificates = bool.fromEnvironment('ALLOW_BAD_CERTIFICATES');
if (allowBadCertificates) {
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () => HttpClient()
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true,
);
}
dio.interceptors.addAll([
CookieManager(cookieJar), // Store cookies
HttpsInterceptor(), // Enforce HTTPS
RedirectInterceptor(() => dio), // Handle redirects within this Dio instance
LogInterceptor(), // Log requests and responses
]);
return dio;
}