json_store 1.0.1+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

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

[1.0.1+1] - 2019-09-21

  • Fix readme

[1.0.1] - 2019-09-21

  • Add ability to provide optional database name
  • Updated readme, explaining singleton

[1.0.0] - 2019-09-21

  • Initial Release

example/README.md

examples #

A new Flutter project.

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:


dependencies:
  json_store: ^1.0.1+1

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:json_store/json_store.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/json_store.dart.

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
encrypt ^3.3.1 3.3.1
flutter 0.0.0
flutter_secure_storage ^3.3.1+1 3.3.1+1
path_provider ^1.3.0 1.4.0
sqflite ^1.1.6+4 1.1.7+1
Transitive dependencies
args 1.5.2
asn1lib 0.5.11
charcode 1.1.2
clock 1.0.1
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
meta 1.1.7
path 1.6.4
platform 2.2.1
pointycastle 1.0.1
sky_engine 0.0.99
synchronized 2.1.0+1
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test