network_cache_interceptor 3.0.0
network_cache_interceptor: ^3.0.0 copied to clipboard
NetworkCacheInterceptor is a custom interceptor designed to optimize network requests by integrating caching functionality into your application using the Dio library.
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:network_cache_interceptor/network_cache_interceptor.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// Creating a Dio instance and attaching the NetworkCacheInterceptor
final Dio dio = Dio()
..interceptors.add(
NetworkCacheInterceptor(
noCacheStatusCodes: [401, 403, 304],
noCacheHttpMethods: ['POST', 'PUT'],
cacheValidity: const Duration(minutes: 30),
getCachedDataWhenError: true,
// storeOnlyOptIn defaults to true: only requests that opt into caching
// (extra['cache'] set) are written to disk.
maxEntries: 200, // Optional: cap the number of stored entries.
// Optional: encrypt cached keys and data at rest (1-32 chars).
// encryptionKey: 'my_secret_key',
// Optional: only cache responses that match a custom rule.
// cacheWhen: (r) => r.data is Map && r.data['success'] == true,
),
);
MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Network Cache Example'),
),
body: Center(
child: FutureBuilder(
future: fetchData(),
builder: (context, snapshot) {
// Show a loading indicator while fetching data
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
// Show an error message if fetching fails
else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
// Display fetched data
else {
return Text('Data: ${snapshot.data}');
}
},
),
),
),
);
}
/// Fetches data from the API with caching enabled
Future<String> fetchData() async {
try {
final response = await dio.get(
'https://jsonplaceholder.typicode.com/posts/1',
options: Options(
extra: {
'cache': true, // Enable caching
'validate_time': 60, // Cache validity duration (in minutes)
},
),
);
return response.data.toString();
} catch (e) {
return 'Failed to fetch data';
}
}
}