persistent_state 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

Persistent state #

pub package

Persist state in an Hive database across restarts and returns from hibernation

Usage #

Create state update types #

Define the state mutations:

   enum UpdateType { intProp, stringProp, doubleProp, listProp, mapProp }

Create the state class #

Create your state class and map the properties to the persitent store:

   import 'package:persistent_state/persistent_state.dart';

   enum UpdateType { intProp, stringProp, doubleProp, listProp, mapProp }

   class AppState with PersistentState<UpdateType> {
      int get intProp => select<int>("int_prop");
      set intProp(int v) => mutate<int>("int_prop", v, UpdateType.intProp);
      double get doubleProp => select<double>("double_prop");
      set doubleProp(double v) =>
          mutate<double>("double_prop", v, UpdateType.doubleProp);
      String get stringProp => select<String>("string_prop");
      set stringProp(String v) =>
          mutate<String>("string_prop", v, UpdateType.stringProp);
      List<int> get listProp => select<List<int>>("list_prop");
      set listProp(List<int> v) =>
          mutate<List<int>>("list_prop", v, UpdateType.listProp);
      Map<String, int> get mapProp => select<Map<String, int>>("map_prop");
      set mapProp(Map<String, int> v) =>
          mutate<Map<String, int>>("map_prop", v, UpdateType.mapProp);

All the mutations will be persisted to the database:

   final AppState state = AppState();
   state.doubleProp = 3.0;

Note: the state reads with select do not hit the database and use an in memory copy of the state

Listen to state changes #

A changefeed is available:

   /// [appState] is an [AppState] instance
   appState.changeFeed.listen((StateUpdate change) =>
      print("State update: \n${change.type}");

Example #

An example with Provider is available

Changelog #

0.3.0 #

Move to Hive for storage

0.2.0 #

Refactor to use Kvsql

0.1.0 #

Initial release


import 'package:flutter/material.dart';
import 'package:pedantic/pedantic.dart';
import 'package:provider/provider.dart';

import 'page.dart';
import 'state.dart';
import 'store.dart';

final Map<String, Page Function(BuildContext)> routes = {
  '/': (BuildContext context) => Page(),

void main() {
  appState = AppState();

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return StreamProvider<Store>.value(
        initialData: Store(),
        child: MaterialApp(
          routes: routes,
          debugShowCheckedModeBanner: false,
          title: 'Persistent state example',

Use this package as a library

1. Depend on it

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

  persistent_state: ^0.3.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';
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


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
cupertino_icons ^0.1.2 0.1.3
err ^0.9.0 0.9.0
extra_pedantic ^1.1.1+1 1.2.0
flutter 0.0.0
hive ^1.4.1+1 1.4.1+1
hive_flutter ^0.3.0+2 0.3.0+2
pedantic ^1.8.0 1.9.0
Transitive dependencies
charcode 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
meta 1.1.8
path 1.6.4
path_provider 1.6.5
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