搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目