搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案