DioExtended constructor

DioExtended({
  1. required String baseUrl,
  2. Map<String, String>? headers,
  3. int tokenExpiredCode = 401,
  4. Duration timeout = const Duration(minutes: 1),
  5. String? globalErrorMessage,
  6. String? globalErrorNetworkingMessage,
})

Creates a new DioExtended instance with standardized configuration.

The baseUrl parameter is required and defines the root endpoint used for all HTTP requests made through this instance.

You may optionally provide custom headers to be applied globally (e.g., Authorization, Content-Type, or any other HTTP header).

The tokenExpiredCode is the HTTP status code that indicates the user's authentication has expired. By default, it uses 401.

The timeout determines the maximum time allowed for each request before it triggers a timeout exception. Default is 1 minute.

The globalErrorMessage is a generic fallback error message used when unexpected or unknown errors occur.

The globalErrorNetworkingMessage is used specifically when network conditions fail (e.g., no internet connection, DNS failure, or timeout).

Implementation

DioExtended({
  required String baseUrl,
  Map<String, String>? headers,
  this.tokenExpiredCode = 401,
  this.timeout = const Duration(minutes: 1),
  this.globalErrorMessage,
  this.globalErrorNetworkingMessage,
}) {
  _dio = Dio(
    BaseOptions(
      baseUrl: baseUrl,
      connectTimeout: timeout,
      receiveTimeout: timeout,
      sendTimeout: timeout,
      headers: {'Accept': 'application/json', ...?headers},
    ),
  );

  // Add debug logging interceptor (only active in debug mode)
  _dio.interceptors.add(const LogApiInterceptor());

  // Add chucker interceptor
  _dio.interceptors.add(ChuckerDioInterceptor());

  // Add token refresh interceptor if a callback is provided
  _dio.interceptors.add(
    DioInterceptor(
      dio: _dio,
      refreshTokenCallback: () async => await handleTokenExpired(),
      tokenExpiredCode: tokenExpiredCode,
    ),
  );
}