simple_json_persistence 0.2.0

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 {
  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));
  await '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: ${ ?? 'Loading'}'),

[0.2.0] - 2019-01-07

  • Upgrade to rxdart 0.23

[0.1.1+2] - 2019-09-16

  • Possible bug fix not correctly caching loaded files.

[0.1.1+1] - 2019-08-19

  • Added LICENSE file and more documentation.

[0.1.1] - 2019-08-09

  • Allow custom names for storage.
  • Added simple example application.
  • More test coverage.

[0.1.0+1] - dartfmt

[0.1.0] - 2019-08-09

  • Initial Release


example #

Example Application using simple_json_persistence

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  simple_json_persistence: ^0.2.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:simple_json_persistence/simple_json_persistence.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 12, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7

Health suggestions

Format lib/src/simple_json_persistence.dart.

Run flutter format to format lib/src/simple_json_persistence.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
flutter 0.0.0
logging >=0.10.0 <1.0.0 0.11.4
meta >=1.1.0 <2.0.0 1.1.8
path >=1.5.0 <2.0.0 1.6.4
path_provider >=1.1.0 <2.0.0 1.6.0
rxdart >=0.23.0 <1.0.0 0.23.1
Transitive dependencies
collection 1.14.11 1.14.12
platform 2.2.1
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
pedantic ^1.8.0+1