network_cache_interceptor 0.0.1
network_cache_interceptor: ^0.0.1 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(const MyApp());
}
class MyApp extends StatelessWidget {
final Dio dio = Dio()..interceptors.add(NetworkCacheInterceptor());
const 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) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Text('Data: ${snapshot.data}');
}
},
),
),
),
);
}
Future<String> fetchData() async {
try {
final response = await dio.get(
'https://jsonplaceholder.typicode.com/posts/1',
options: Options(
extra: {
'cache': true,
'validate_time': 60, // Cache validity in minutes
},
),
);
return response.data.toString();
} catch (e) {
return 'Failed to fetch data';
}
}
}