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

源码仓库

使用此插件的项目

暂无相关数据。