network_cache_interceptor 1.2.4 copy "network_cache_interceptor: ^1.2.4" to clipboard
network_cache_interceptor: ^1.2.4 copied to clipboard

NetworkCacheInterceptor is a custom interceptor designed to optimize network requests by integrating caching functionality into your application using the Dio library.

Network Cache Interceptor #

Network Cache Interceptor is a custom Dio interceptor designed for caching network requests. It returns cached data when offline and optimizes network request handling.


πŸ“¦ Installation #

Add the following line to your pubspec.yaml:

dependencies:
  network_cache_interceptor: ^1.2.4

Or install it using flutter pub add:

flutter pub add network_cache_interceptor

πŸš€ What’s New in Version 1.2.4 #

  • Updated Caching Logic:
    In version 1.2.4, the caching logic has been enhanced. All GET requests are now cached by default, even if cache: false is explicitly specified. This ensures consistent caching while maintaining manual control through additional options.

πŸš€ Usage #

1. Configure Dio #

import 'package:dio/dio.dart';
import 'package:network_cache_interceptor/network_cache_interceptor.dart';

void main() {
  final dio = Dio();

  // Attach the interceptor
  dio.interceptors.add(
    NetworkCacheInterceptor(
      noCacheStatusCodes: [401, 403],
      cacheValidityMinutes: 30,
      getCachedDataWhenError: true,
    ),
  );
}

2. Make a Request #

All GET requests are now cached by default, but you can still override caching behavior using extra['cache']:

final response = await dio.get(
  'https://jsonplaceholder.typicode.com/posts',
  options: Options(
    extra: {
      'cache': true,         // Explicitly enable caching
      'validate_time': 60,   // Cache validity time (minutes)
    },
  ),
);

To disable caching manually:

final response = await dio.get(
  'https://jsonplaceholder.typicode.com/posts',
  options: Options(
    extra: {'cache': false}, // Disable caching
  ),
);

3. Clear Cached Data #

Clear all cached data from the database:

final cacheInterceptor = NetworkCacheInterceptor();
await cacheInterceptor.clearDatabase();

βš™οΈ Configuration #

Parameter Description Default Value
noCacheStatusCodes Status codes not to be cached [401, 403]
cacheValidityMinutes Cache validity time (minutes) 30
getCachedDataWhenError Fetch cached data when offline true

πŸ”§ Technical Details #

  • Caching Logic: If there's a network issue, previously cached responses are automatically returned if available.
  • Error Logging: All errors are logged using log().
  • Data Storage: Data is saved locally using an SQL database.

🎯 Example #

final dio = Dio();
dio.interceptors.add(NetworkCacheInterceptor());

try {
  final response = await dio.get(
    'https://jsonplaceholder.typicode.com/posts',
    options: Options(extra: {'cache': true}),
  );
  print(response.data);
} catch (e) {
  print('Error: $e');
}

πŸ›‘οΈ License #

This project is licensed under the MIT License.


πŸ’¬ Additional Information #

For more information or to contribute, visit our GitHub page.


Stay updated on new releases and project announcements! πŸŽ‰


πŸ“¦ Check Out My Other Packages #

If you find this package useful, you might also be interested in:

  • Telegram Bot Crashlytics - A comprehensive error logging package that sends application crashes and errors directly to a Telegram chat.

Stay connected for more powerful and easy-to-use packages! πŸš€

8
likes
160
points
396
downloads

Publisher

unverified uploader

Weekly Downloads

NetworkCacheInterceptor is a custom interceptor designed to optimize network requests by integrating caching functionality into your application using the Dio library.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, path, sqflite

More

Packages that depend on network_cache_interceptor