Setting up Dio for network requests in a Flutter application

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Setting up Dio for network requests in a Flutter application
Medium
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1052
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Setting up Dio for network requests in Flutter applications

The http package from the Dart ecosystem covers basic needs, but production applications quickly face tasks it solves awkwardly: authorization interceptors, automatic retry, logging, request cancellation, multipart file uploads with progress. Dio covers all this out of the box.

Installation and basic configuration

dependencies:
  dio: ^5.4.0

Create a singleton via get_it or riverpod:

final dio = Dio(BaseOptions(
  baseUrl: 'https://api.example.com/v1',
  connectTimeout: const Duration(seconds: 10),
  receiveTimeout: const Duration(seconds: 30),
  headers: {'Accept': 'application/json'},
));

Interceptors — key part of configuration

Auth interceptor adds token to each request and handles 401:

class AuthInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    final token = tokenStorage.accessToken;
    if (token != null) {
      options.headers['Authorization'] = 'Bearer $token';
    }
    handler.next(options);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) async {
    if (err.response?.statusCode == 401) {
      try {
        await tokenStorage.refresh();
        // Retry original request with new token
        final opts = err.requestOptions;
        opts.headers['Authorization'] = 'Bearer ${tokenStorage.accessToken}';
        final response = await dio.fetch(opts);
        handler.resolve(response);
        return;
      } catch (_) {
        // refresh failed — logout
      }
    }
    handler.next(err);
  }
}

Logging in dev mode:

if (kDebugMode) {
  dio.interceptors.add(LogInterceptor(
    requestBody: true,
    responseBody: true,
    logPrint: (o) => debugPrint(o.toString()),
  ));
}

Retry interceptor for network errors — use dio_smart_retry:

dio.interceptors.add(RetryInterceptor(
  dio: dio,
  retries: 3,
  retryDelays: [
    Duration(seconds: 1),
    Duration(seconds: 2),
    Duration(seconds: 3),
  ],
));

File uploads and request cancellation

// Multipart upload with progress
final formData = FormData.fromMap({
  'file': await MultipartFile.fromFile(filePath, filename: 'photo.jpg'),
});

await dio.post('/upload', data: formData,
  onSendProgress: (sent, total) {
    progress.value = sent / total;
  },
);

// Cancel request
final cancelToken = CancelToken();
dio.get('/data', cancelToken: cancelToken);
// Later:
cancelToken.cancel('User navigated away');

CancelToken is mandatory for requests tied to widget lifecycle. Not canceling requests in dispose() — memory leak and possible setState after dispose.

Error handling

DioException contains type — important to distinguish:

  • DioExceptionType.connectionTimeout — no internet or server unavailable
  • DioExceptionType.badResponse — server returned 4xx/5xx
  • DioExceptionType.cancel — request canceled

Wrap in domain layer so you don't drag Dio dependency into BLoC/Cubit:

Future<Either<Failure, T>> safeCall<T>(Future<T> Function() request) async {
  try {
    return Right(await request());
  } on DioException catch (e) {
    return Left(NetworkFailure.fromDioException(e));
  }
}

Timelines

Basic Dio setup with auth interceptor and logging: 4–8 hours. With retry, error handling and architecture integration (BLoC, Riverpod): 1–2 days. Pricing calculated individually.