パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト