redux_persist_flutter 0.5.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 96

redux_persist_flutter pub package

Flutter Storage Engine for redux_persist.

Can either save to shared_preferences (default, recommended), or your application document directory.

Usage

var persistor = new Persistor<State>(
  // ...
  storage: new FlutterStorage("my-app"),
);

PersistorGate

If you want to wait until rendering you app until the state is loaded, use the PersistorGate:

@override
Widget build(BuildContext context) {
  return new PersistorGate(
    persistor: persistor,
    builder: (context) => MyApp(),
  );
}

If you want to display a loading/slash screen while loading, pass a widget to render to the loading param of PersistorGate:

new PersistorGate(
  persistor: persistor,
  loading: SlashScreen(), // !!!
  builder: (context) => MyApp(),
);

Locations

By default, it saves to FlutterSaveLocation.sharedPreference (shared_preferences, recommended).

You can also save to your application document directory by using FlutterSaveLocation.documentFile:

new FlutterStorage("my-app", location: FlutterSaveLocation.documentFile)

Features and bugs

Please file feature requests and bugs at the issue tracker.

[0.5.0] - 2018-03-18

Breaking:

  • Fix typo sharedPreference to sharedPreferences (and SharedPreferenceEngine to SharedPreferencesEngine). You will need to rename if you are using it explicitly.

[0.4.6] - 2018-03-14

  • Fix library export.

[0.4.5] - 2018-03-13

  • Update load listener from loadStream.listen to Future.

[0.4.4] - 2018-03-12

  • Update redux_persist dependency.

[0.4.3] - 2018-03-12

  • Fix load bug (loaded JSON instead of string)

[0.4.2] - 2018-03-10

  • Breaking: Change PersistorGate child to builder.

[0.4.1] - 2018-03-10

  • Breaking: Change PersistorGate from Widget to WidgetBuilder.

[0.4.0] - 2018-03-10

  • Initial decoupling.

example/redux_persist_flutter_example.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux_persist/redux_persist.dart';
import 'package:redux_persist_flutter/redux_persist_flutter.dart';

void main() {
  runApp(new MyApp());
}

// Redux
class AppState {
  final int counter;

  AppState({this.counter = 0});

  AppState copyWith({int counter}) {
    return new AppState(counter: counter ?? this.counter);
  }

  static AppState fromJson(dynamic json) {
    return new AppState(counter: json["counter"] as int);
  }

  dynamic toJson() => {'counter': counter};
}

class IncrementCounterAction {}

AppState reducer(AppState state, Object action) {
  if (action is LoadedAction<AppState>) {
    // Load to state
    return action.state ?? state;
  } else if (action is IncrementCounterAction) {
    // Increment
    return state.copyWith(counter: state.counter + 1);
  }

  return state;
}

// App
class MyApp extends StatelessWidget {
  Persistor<AppState> persistor;
  Store<AppState> store;

  MyApp() {
    // Create Persistor
    persistor = new Persistor<AppState>(
      storage: new FlutterStorage("my-app"),
      decoder: AppState.fromJson,
    );

    store = new Store<AppState>(
      reducer,
      initialState: new AppState(),
      middleware: [persistor.createMiddleware()],
    );

    // Load state and dispatch LoadAction
    persistor.start(store);
  }

  @override
  Widget build(BuildContext context) {
    // PersistorGate waits for state to be loaded before rendering
    return new PersistorGate(
      persistor: persistor,
      builder: (context) => new StoreProvider(
            store: store,
            child: new MaterialApp(
                title: 'Redux Persist Demo', home: new MyHomePage()),
          ),
    );
  }
}

// Counter example
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Redux Persist demo"),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new StoreConnector<AppState, String>(
              converter: (store) => store.state.counter.toString(),
              builder: (context, count) => new Text(
                    '$count',
                    style: Theme.of(context).textTheme.display1,
                  ),
            ),
          ],
        ),
      ),
      floatingActionButton: new StoreConnector<AppState, VoidCallback>(
        converter: (store) {
          // Return a `VoidCallback`, which is a fancy name for a function
          // with no parameters. It only dispatches an Increment action.
          return () => store.dispatch(new IncrementCounterAction());
        },
        builder: (context, callback) => new FloatingActionButton(
              onPressed: callback,
              tooltip: 'Increment',
              child: new Icon(Icons.add),
            ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  redux_persist_flutter: "^0.5.0"

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


      import 'package:redux_persist_flutter/redux_persist_flutter.dart';
  
Version Uploaded Documentation Archive
0.5.0 Mar 19, 2018 Go to the documentation of redux_persist_flutter 0.5.0 Download redux_persist_flutter 0.5.0 archive
0.4.6 Mar 14, 2018 Go to the documentation of redux_persist_flutter 0.4.6 Download redux_persist_flutter 0.4.6 archive
0.4.5 Mar 13, 2018 Go to the documentation of redux_persist_flutter 0.4.5 Download redux_persist_flutter 0.4.5 archive
0.4.4 Mar 12, 2018 Go to the documentation of redux_persist_flutter 0.4.4 Download redux_persist_flutter 0.4.4 archive
0.4.3 Mar 12, 2018 Go to the documentation of redux_persist_flutter 0.4.3 Download redux_persist_flutter 0.4.3 archive
0.4.2 Mar 10, 2018 Go to the documentation of redux_persist_flutter 0.4.2 Download redux_persist_flutter 0.4.2 archive
0.4.1 Mar 10, 2018 Go to the documentation of redux_persist_flutter 0.4.1 Download redux_persist_flutter 0.4.1 archive
0.4.0 Mar 10, 2018 Go to the documentation of redux_persist_flutter 0.4.0 Download redux_persist_flutter 0.4.0 archive
0.6.0-rc.1 Mar 31, 2018 Go to the documentation of redux_persist_flutter 0.6.0-rc.1 Download redux_persist_flutter 0.6.0-rc.1 archive

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3
  • Flutter: 0.5.4

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
91 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
96
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.0 <2.0.0
flutter 0.0.0
path_provider ^0.3.1 0.3.1 0.4.1
redux_persist ^0.6.0 0.6.0 0.7.0-rc.2
shared_preferences ^0.4.0 0.4.2
Transitive dependencies
collection 1.14.6 1.14.10
meta 1.1.5
sky_engine 0.0.99
typed_data 1.1.5
vector_math 2.0.6 2.0.7
Dev dependencies
flutter_redux ^0.3.5
redux ^2.1.1 2.1.1 3.0.0