data_cache_manager 1.2.0 copy "data_cache_manager: ^1.2.0" to clipboard
data_cache_manager: ^1.2.0 copied to clipboard

A Flutter cache manager plugin for storing and managing Dart data types.

example/lib/main.dart

import 'package:data_cache_manager/data_cache_manager.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _manager = DefaultDataCacheManager.instance;
  final String _key = 'key';
  CachedData _cachedData;

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Data Cache Manager'),
        ),
        body: Center(
          child: Builder(
            builder: (context) {
              if (_cachedData != null) {
                return Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Cached data: ${_cachedData.value}\n'),
                        Text('Cache location: ${_cachedData.location}\n'),
                        Text('Updated at: ${_cachedData.updatedAt}\n'),
                        Text('Last used at: ${_cachedData.lastUsedAt}\n'),
                        Text('Use count: ${_cachedData.useCount}\n'),
                      ],
                    ),
                    ElevatedButton(
                      onPressed: () async {
                        setState(() => _cachedData = null);
                        final data = await _manager.get(_key);
                        setState(() => _cachedData = data);
                      },
                      child: Text('Refresh'),
                    ),
                    SizedBox(height: 16),
                    ElevatedButton(
                      onPressed: () async {
                        setState(() => _cachedData = null);
                        final now = DateTime.now();
                        final data = await _manager.get(_key, updatedAt: now);
                        setState(() => _cachedData = data);
                        await _fetchData();
                      },
                      child: Text('Update'),
                    ),
                  ],
                );
              }

              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }

  // Mock data fetch from some API
  Future<void> _fetchData() async {
    await _manager.clear();
    await Future.delayed(const Duration(seconds: 2));
    final data = await _manager.add(_key, 'data');
    setState(() => _cachedData = data);
  }
}
4
likes
130
pub points
68%
popularity

Publisher

unverified uploader

A Flutter cache manager plugin for storing and managing Dart data types.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

copy_with_extension, equatable, flutter, json_annotation, ntp, path, sqflite

More

Packages that depend on data_cache_manager