persistent_state 0.1.0

Persistent state #

pub package

Persist state in an Sqlite database across restarts and returns from hibernation. Powered by Sqlcool

Api #

Constructor #

PersistentState: constructor parameters:

  • db: an Sqlcool database: see the documentation
  • table: the table to be used for the state. Defailt "state".
  • id: id of the table row to use. Default 1.
  • verbose: verbosity level

Methods #

init: initialize the state. Run before using it.

onReady: a future that will complete when the state is initialized

mutate: change the value of a key in the persistent state

  • key: the key to modify
  • value: the new value

This method is asynchronous but can not be awaited. The database queries are queued and will be exectuted in order in case of multiple calls to this method.

select: get the value of a key

  • key: the key to get

This method does not hit the database.

dispose: dispose the state once finished using to clean up memory

describe: prints a description of the state

Example #

Goal: to persist the current page

Define the state data and initialize the database #

In db.dart:

   import 'package:sqlcool/sqlcool.dart';

   Db db = Db();

   DbTable stateTableSchema() {
     /// Define a state table in the database
     ///
     /// Documentation about schemas:
     /// https://pub.dev/documentation/sqlcool/latest/sqlcool/DbTable-class.html
     ///
     return DbTable("state")..varchar("route", defaultValue: '"/page1"');
   }

   Future<void> initDb() async {
      try {
         await db.init(
            path: "db.sqlite",
            schema: [stateTableSchema()],
            queries: [_populate()]);
         } catch (e) {
      throw ("Can not init db $e");
      }
   }

   String _populate() {
      return 'INSERT INTO state(id) VALUES(1)';
   }

Create the state #

In state.dart:

   import 'dart:async';
   import 'package:flutter/material.dart';
   import 'package:persistent_state/persistent_state.dart';
   import 'db.dart';

   AppState state = AppState();

   class AppState {
      PersistentState store;

      Completer _readyCompleter = Completer();

      String get currentRoute => store.select("route");

      Future<dynamic> get onReady => _readyCompleter.future;

      Future<void> navigate(BuildContext context, String routeName) async {
         // Hit the database to update the route
         store.mutate("route", routeName);
         await Navigator.of(context).pushNamed(routeName);
      }

      Future<void> init() async {
         /// db is an Sqlcool [Db] object
         /// run [initDb] before this
         assert(db.isReady);
         try {
            store = PersistentState(db: db);
            store.init();
            await store.onReady;
            _readyCompleter.complete();
         } catch (e) {
            throw ("Can not create persistent state $e");
         }
      }
   }

Init state #

   void main() {
      initDb().then((_) => state.init());
      runApp(MyApp());
   }

   // later
   await state.onReady;
   // or
   state.onReady.then((_) => doSomething());

Use the state #

Calling navigate from anywhere will persist the state of the current route

   state.navigate(context, "/page3");

Changelog #

0.1.0 #

Initial release

example/README.md

Example #

Goal: to persist the current page

Define the state data and initialize the database #

In db.dart:

   import 'package:sqlcool/sqlcool.dart';

   Db db = Db();

   DbTable stateTableSchema() {
     // define a state table in the database
     return DbTable("state")..varchar("route", defaultValue: '"/page1"');
   }

   Future<void> initDb() async {
      try {
         await db.init(
            path: "db.sqlite",
            schema: [stateTableSchema()],
            queries: [_populate()]);
         } catch (e) {
      throw ("Can not init db $e");
      }
   }

   String _populate() {
      return 'INSERT INTO state(id) VALUES(1)';
   }

Create the state #

In state.dart:

   import 'dart:async';
   import 'package:flutter/material.dart';
   import 'package:persistent_state/persistent_state.dart';
   import 'db.dart';

   AppState state = AppState();

   class AppState {
      PersistentState store;

      Completer _readyCompleter = Completer();

      String get currentRoute => store.select("route");

      Future<dynamic> get onReady => _readyCompleter.future;

      Future<void> navigate(BuildContext context, String routeName) async {
         store.mutate("route", routeName);
         await Navigator.of(context).pushNamed(routeName);
      }

      Future<void> init() async {
         /// db is an Sqlcool [Db] object
         /// run [initDb] before this
         assert(db.isReady);
         try {
            store = PersistentState(db: db);
            await store.init();
            await store.onReady;
            _readyCompleter.complete();
         } catch (e) {
            throw ("Can not create persistent state");
         }
      }
   }

Use the state #

Calling navigate from anywhere will persist the state of the current route

   state.navigate(context, "/page3");

Use this package as a library

1. Depend on it

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


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

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/persistent_state.dart. (-0.50 points)

Analysis of lib/src/persistent_state.dart reported 1 hint:

line 93 col 57: 'schema' is deprecated and shouldn't be used.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (sqlcool).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
cupertino_icons ^0.1.2 0.1.2
flutter 0.0.0
sqlcool ^2.8.1 2.9.0 3.1.1
Transitive dependencies
collection 1.14.11
dart_internal 0.1.5
matcher 0.12.5
meta 1.1.6 1.1.7
observable 0.22.2
path 1.6.2
path_provider 1.1.2
quiver 2.0.3
sky_engine 0.0.99
sqflite 1.1.6+2
stack_trace 1.9.3
synchronized 2.1.0+1
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin