搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.4

json_store

一種類似於瀏覽器 localStorage 的 Flutter 儲存解決方案(但功能更強大)

AndroidiOSmacOS
近 30 天下載
2590
喜歡數
39
Pub 分數
150

安裝套件

flutter pub add json_store

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

json_store 是一個 Flutter 套件,提供一種受瀏覽器 localStorage 啟發的持久性、類似 NoSQL 的儲存解決方案。它可在 Android、iOS 和 macOS 上輕鬆儲存和檢索跨應用重啟的 JSON 可序列化資料。設計簡潔高效,支援非同步操作和自動序列化/反序列化,非常適合儲存使用者偏好設定、快取資料或小型資料集,而無需設定完整資料庫。

適用情境

  • 儲存使用者偏好
  • 快取 API 回應
  • 持久化應用狀態
  • 儲存表單資料
  • 管理本地設定
  • 處理離線資料

主要特色

  • 基於JSON的儲存
  • 跨平台支援
  • 非同步作業
  • 自動序列化
  • 簡單 API
  • 輕巧且快速

適合對象

  • 中小型資料儲存
  • 需要離線功能的應用程式
  • 原型設計或最小可行產品開發
  • 簡單數據持久化需求
  • 開發者避開複雜資料庫

使用建議

  • 不適用於大型資料集
  • 有限的查詢功能
  • 無內建資料同步
  • 最適合簡單的鍵值對模式
  • 不支援複雜關係

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

json_store

A Storage solution somewhat similar to a browser's localStorage in the sence that it is a key value pair storage. The value should be a dart reprisentation of a JSON Object (aka a Map<String, dynamic>)

Features

  • Data is stored in sqflite Database
  • Data may be fully encrypted (encrypt)
  • Encryption keys are stored in Keychain and Keystore thanks to flutter_secure_storage
  • Because the DB schema never changes, there is no concern about database migrations
  • Batch processing
  • Data stored has a time to live. Whenever a getItem is done on a key which ttl has expired, that key will be deleted and a null value will be returned

Getting Started

In your flutter project add the dependency:

dependencies:
  ...
  json_store:
  ...

For help getting started with Flutter, view the online documentation.

Usage example

See example folder for full sample app

Note: JsonStore is a singleton and instantiating with factory constructor JsonStore() will only ever have one instance

Simple example
import 'package:json_store/json_store.dart';

class CounterModel {
  int value;
  CounterModel(this.value);
  CounterModel.fromJson(Map<String, dynamic> json) : this.value = json['value'];
  Map<String, dynamic> toJson() => {'value': value};
}

...

JsonStore jsonStore = JsonStore();
CounterModel counter;

loadFromStorage() async {
  Map<String, dynamic> json = await jsonStore.getItem('counter');
  counter = json != null ? CounterModel.fromJson(json) : CounterModel(0);
}

incrementCounter() async {
  counter.value++;
  await jsonStore.setItem('counter', counter.toJson());
}

...
Batch example

When adding a lot of data at a time (Like a long list of messages), use the batch feature as it will improve performance significantly

class Message {
  final int id;
  final String title;
  final String body;
  
  Message([this.id, this.title, this.body]);
  Message.fromJson(Map<String, dynamic> json)
      : this.id = json['id'],
        this.title = json['title'],
        this.body = json['body'];
  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'body': body,
      };
}

saveBigList() {
  Batch batch = await jsonStore.startBatch();
  await Future.forEach(_messages, (message) async {
    await jsonStore.setItem(
      'messages-${message.id}',
      message.toJson(),
      batch: batch,
    );
  });
  jsonStore.commitBatch(batch);
}

loadBigList() {
      List<Map<String, dynamic>> json = await jsonStore.getListLike('messages-%');

    messages = json != null
        ? json.map((messageJson) => Message.fromJson(messageJson)).toList()
        : [];

}

Configure Android

In [project]/android/app/build.gradle set minSdkVersion to >= 18.

android {
    ...
    defaultConfig {
        ...
        minSdkVersion 18
        ...
    }
}
Thanks

Thanks to contributors

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。