simple_json_persistence 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

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


import 'package:example/logging.dart';
import 'package:example/model.dart';
import 'package:flutter/material.dart';
import 'package:simple_json_persistence/simple_json_persistence.dart';

void main() {

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
      home: SimpleCounter(),

class SimpleCounter extends StatelessWidget {
  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) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('SimpleJsonPersistence Example'),
            body: Center(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: !snapshot.hasData
                      ? <Widget>[Center(child: const CircularProgressIndicator())]
                      : <Widget>[
                          const Text(
                            'You have pushed the button:',
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                                style: Theme.of(context).textTheme.display1,
                              const Text(' times')
                          const SizedBox(height: 32),
                          const Text(
                            'Value will be persistet on every touch, so feel free to restart the app at any time.',
                            textScaleFactor: 0.75,
            floatingActionButton: FloatingActionButton(
              onPressed: () => + 1)),
              tooltip: 'Increment',
              child: Icon(Icons.add),

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 Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

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.5
rxdart >=0.23.0 <1.0.0 0.23.1 0.24.0-dev.1
Transitive dependencies
collection 1.14.11 1.14.12
path_provider_macos 0.0.4
path_provider_platform_interface 1.0.1
platform 2.2.1
plugin_platform_interface 1.0.2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
pedantic ^1.8.0+1