cached_future_builder_plus 0.0.1
cached_future_builder_plus: ^0.0.1 copied to clipboard
A Flutter widget that extends FutureBuilder with intelligent caching capabilities.
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:cached_future_builder_plus/cached_future_builder_plus.dart';
void main() {
runApp(const CachedFutureBuilderExample());
}
class CachedFutureBuilderExample extends StatelessWidget {
const CachedFutureBuilderExample({super.key});
Future<Map<String, dynamic>?> _fetchTodo() async {
try {
final response = await Dio().get(
'https://jsonplaceholder.typicode.com/todos/1',
options: Options(headers: {'Content-Type': 'application/json'}),
);
return response.data;
} catch (e) {
debugPrint('Error: $e');
return null;
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Example App')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16),
child: CachedFutureBuilderPlus<Map<String, dynamic>?>(
cacheKey: 'todo:1',
cachePolicy: CachePolicy.cacheOnly,
initialData: null,
future: _fetchTodo(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
final data = snapshot.data;
if (data == null) {
return const Text('No data available');
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Title: ${data['title']}'),
const SizedBox(height: 8),
Text('Completed: ${data['completed']}'),
const SizedBox(height: 16),
const Text(
'Tip: First load may show cached result immediately, then update from network.',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
],
);
},
),
),
),
),
);
}
}