Search packages, repositories and app categories

Package detailsv2.2.0

simple_json_persistence

Flutter data storage based on simple json files with support for multiple storage instances, streams, etc.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
296
Likes
5
Pub points
150

Install package

flutter pub add simple_json_persistence

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A Flutter package for simple JSON-based data persistence, enabling storage of structured data in JSON files across multiple platforms. It supports multiple independent storage instances, real-time data streams, and seamless integration with Dart's async model. Ideal for apps needing lightweight, file-based storage without complex database overhead. The package is designed for ease of use, with minimal setup and clear APIs for reading, writing, and listening to changes. It’s particularly useful for small to medium-sized applications where full database solutions are unnecessary.

Use cases

  • Storing user preferences
  • Saving app state between sessions
  • Managing local configuration files
  • Persisting small datasets
  • Enabling offline data access

Key features

  • Multiple storage instances
  • Real-time data streaming
  • Cross-platform support
  • Simple API
  • File-based JSON storage

Suitable for

  • Lightweight data storage needs
  • Prototyping and small apps
  • Offline-first applications
  • Apps avoiding complex databases

Considerations

  • Not suitable for large datasets
  • Limited query capabilities
  • No built-in encryption
  • Performance may degrade with very large files

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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'}'),
  );
}

Categories

Dependencies

Dependency list 8 items

Source repository

Projects using this package