api_cache_plugin 0.0.1 copy "api_cache_plugin: ^0.0.1" to clipboard
api_cache_plugin: ^0.0.1 copied to clipboard

A Flutter plugin for caching API responses using Hive and GZip compression with configurable timeouts.

example/main.dart

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:hive_flutter/hive_flutter.dart';
import 'package:api_cache_plugin/api_cache_plugin.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  setCacheTimeout(const Duration(minutes: 5));

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final cache = CacheManager();
  String data = "Loading...";

  Future<void> fetchData() async {
    const url = "https://jsonplaceholder.typicode.com/posts/1";

    final cached = await cache.getCached(url);
    if (cached != null) {
      setState(() => data = "📦 From Cache:\n\n$cached");
      return;
    }

    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      await cache.saveCache(url, response.body);
      setState(() => data = "🌐 From Network:\n\n${response.body}");
    }
  }

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("API Cache Plugin Example")),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SingleChildScrollView(child: Text(data)),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: fetchData,
        child: const Icon(Icons.refresh),
      ),
    );
  }
}
1
likes
130
points
58
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for caching API responses using Hive and GZip compression with configurable timeouts.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

archive, flutter, hive, hive_flutter, path_provider

More

Packages that depend on api_cache_plugin