搜索插件、代码仓与 APP 分类

插件详情v0.1.0

flutter_cache

一个简单的 Flutter 缓存包。此包是 shared preference 的包装器,使使用 shared preference 更加轻松。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1860
喜欢数
22
Pub 分数
120

安装插件

flutter pub add flutter_cache

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_cache 是一个轻量级包,可简化在 Flutter 应用中使用共享首选项。它作为 shared_preferences 的包装器,提供了一个易于使用的 API,用于在应用重启后持久存储和检索数据。该包设计简洁高效,支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。该包抽象了常见的缓存操作,非常适合管理少量持久化数据,例如用户设置、令牌或缓存响应。

适合场景

  • 存储用户偏好
  • 缓存 API 响应数据
  • 持久化登录令牌
  • 保存应用状态
  • 管理配置设置

主要特点

  • 简单 API
  • 共享偏好包装器
  • 跨平台支持
  • 轻松的数据序列化
  • 轻便且快速

适合对象

  • 初学者Flutter开发者
  • 需要基本持久性的应用
  • 小规模数据存储
  • 快速原型设计
  • 简单的缓存需求

使用建议

  • 不适用于大型数据集
  • 仅限键值存储
  • 无内置过期机制
  • 依赖共享偏好性能
  • 不支持复杂查询

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_cache

Dependencies Status file size GitHub Issues follows on twitter

A simple cache package for flutter. This package is a wrapper for shared preference and makes working with shared preference easier. Once it has been installed, you can do these things.

import 'package:flutter_cache/flutter_cache.dart' as cache;

// create new cache.
cache.remember('key', 'data'); 
cache.write('key', 'data'); 

// add Cache lifetime on create
cache.remember('key', 'data', 120); 
cache.write('key', 'data', 120); 

// load Cache by key
cache.load('key'); // This will load the cache data.

// return `defaultValue` if key not exists
cache.load('key', 'defaultValue')

// destroy single cache by key
cache.destroy('key');

// destroy all cache
cache.clear();

Getting Started

Installation

First include the package dependency in your project's pubspec.yaml file

dependencies:
  flutter_cache: ^0.1.0

You can install the package via pub get:

flutter pub get

Then you can import it in your dart code like so

import 'package:flutter_cache/flutter_cache.dart' as cache;
What Can this Package Do ?
  1. Cache String, Map, List<String> and List<Map> for forever or for a limited time.
  2. Load the cache you've cached.
  3. Clear All Cache.
  4. Clear Single Cache.
Usage
Cache Fetch Data from API

If data already exist, then it will use the data in the cache. If it's not, It will fetch the data. You can also set Cache lifetime so your app would fetch again everytime the Cache dies.

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter_cache/flutter_cache.dart' as cache;

var employees = await cache.remember('employees', () async {
  var response = await http.get('http://dummy.restapiexample.com/api/v1/employees');
  return jsonDecode(response.body)['data'].cast<Map>();
}, 120); // cache for 2 mins

// or 

// cache for 2 mins
var employees = await cache.remember(
    'servers', 
    () async => jsonDecode( 
        (await http.get( 'http://dummy.restapiexample.com/api/v1/employees' )
    ).body )['data'].cast<Map>()
, 120);
Saved data for limited time

The data will be destroyed when it reached the time you set.

cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
cache.write('key', 'data', 120);
Cache multipe datatype

You can cache multiple datatype. Supported datatype for now are String, Map, List<String> and List<Map>. When you use cache.load() to get back the data, it will return the data in the original datatype.

cache.remember('key', { 
  'name' : 'Ashraf Kamarudin',
  'depth2' : {
    'name' : 'depth2',
    'depth3' : {
      'name': 'depth3'
    } 
  }
});

cache.load('key'); // will return data in map datatype.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目