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

套件詳情v2.2.0

simple_json_persistence

基於簡單 JSON 檔案的 Flutter 資料儲存,支援多個儲存實例、串流等。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
296
喜歡數
5
Pub 分數
150

安裝套件

flutter pub add simple_json_persistence

套件介紹

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

一個用於簡單基於 JSON 的資料持久化之 Flutter 套件,可於多個平台將結構化資料儲存在 JSON 檔案中。它支援多個獨立的儲存實例、即時資料串流,並與 Dart 的非同步模型無縫整合。適用於需要輕量級、檔案式儲存而不需要複雜資料庫開銷的應用程式。此套件設計以易用為導向,設定簡單,並提供明確的 API 用於讀取、寫入及監聽變更。特別適合中小型應用程式,無需使用完整資料庫解決方案。

適用情境

  • 儲存使用者偏好
  • 在會話之間保存應用狀態
  • 管理本地設定檔
  • 持久化小數據集
  • 啟用離線資料存取

主要特色

  • 多個儲存實例
  • 即時資料流
  • 跨平台支援
  • 簡單 API
  • 基於檔案的 JSON 儲存

適合對象

  • 輕量級資料儲存需求
  • 原型設計和小型應用
  • 離線優先的應用程式
  • 應用避免使用複雜的資料庫

使用建議

  • 不適用於大型資料集
  • 有限的查詢功能
  • 無內建加密
  • 效能可能會在檔案非常大時下降

原始 README

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

simple_json_persistence

codecov Pub Cirrus CI - Base Branch Build Status

Flutter data storage based on simple json files. The main advantage to using for example the shared preference plugin is that:

  1. It is completely written in dart, so no communication with native ios/android code.
  2. It is possible to have multiple storages for different base types. While the shared preferences load the whole data into memory at once, this allows you to split it up into multiple json storage. (e.g. one small storage for app data/config which needs to be loaded on startup and a larger json storage with larger application data).

It is still not suitable for large datasets, since all data will be kept in memory and written into the json file at once, but it's a good middle ground between shared preferences and complicated database.

Getting Started

To make use of SimpleJsonPersistence you have to create your data models to serialize to and from json. I personally prefer to use built_value with it's serialization, or json_serializable.

Anyway, it is recommended that persistence models are immutable.

import 'package:simple_json_persistence/simple_json_persistence.dart';

@JsonSerializable(nullable: false)
class MyModel {
  MyModel({this.property,});

  factory MyModel.fromJson(Map<String, dynamic> json) =>
      _$MyModelFromJson(json);

  Map<String, dynamic> toJson() => _$MyModelToJson(this);

  final String property;
}

void doSomething() async {
  final store = SimpleJsonPersistence.forType(
    (json) => MyModel.fromJson(json),
    name = 'MyModel',
  );
  await store.save(MyModel(property: 'foo'));
  final foo = await store.load();

  // since every SimpleJsonPersistence for the same type/name is the same instance
  // you can also subscribe to changes.
  store.onValueChanged.listen((newValue) {
    print('got a new value $newValue');
  });
}

Usage in flutter (StreamBuilder)

See also the example application.

Widget build(BuildContext context) {
  final store = SimpleJsonPersistence.forType(
      (json) => AppData.fromJson(json),
      defaultCreator: () => AppData(counter: 0));
  return StreamBuilder<AppData>(
    stream: store.onValueChangedAndLoad,
    initialData: store.cachedValue,
    builder: (context, snapshot) => Text('Counter: ${snapshot.data?.counter ?? 'Loading'}'),
  );
}

所屬分類

依賴

依賴清單 8 項

原始碼儲存庫

使用此套件的專案