redux_persist_flutter 0.6.0

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

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

final persistor = Persistor<State>(
  // ...
  storage: 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 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:

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

Locations

By default, it saves to FlutterSaveLocation.documentFile (application document directory, recommended).

You can also save to your shared preferences by using FlutterSaveLocation.sharedPreferences:

// Use shared preferences
FlutterStorage("my-app", location: FlutterSaveLocation.sharedPreferences)

Features and bugs

Please file feature requests and bugs at the issue tracker.

0.6.0-rc.1 - WIP

  • Breaking: Dart 2.
  • Breaking: Revert back to DocumentFileEngine as default.
  • Refactor.

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:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:redux_persist/redux_persist.dart';
import 'package:redux_persist_flutter/redux_persist_flutter.dart';

void main() => runApp(App());

// Redux
class AppState {
  final int counter;

  AppState({this.counter = 0});

  AppState copyWith({int counter}) =>
      AppState(counter: counter ?? this.counter);

  static AppState fromJson(dynamic json) =>
      AppState(counter: json["counter"] as int);

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

class IncrementCounterAction {}

AppState reducer(AppState state, Object action) {
  if (action is PersistLoadedAction<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 App extends StatelessWidget {
  Persistor<AppState> persistor;
  Store<AppState> store;

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

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

    // Load initial state
    persistor.load(store);
  }

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

// Counter example
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Redux Persist demo"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            StoreConnector<AppState, String>(
              converter: (store) => store.state.counter.toString(),
              builder: (context, count) => Text(
                    '$count',
                    style: Theme.of(context).textTheme.display1,
                  ),
            ),
          ],
        ),
      ),
      floatingActionButton: StoreConnector<AppState, VoidCallback>(
        // Return a function to dispatch an increment action
        converter: (store) => () => store.dispatch(IncrementCounterAction()),
        builder: (_, increment) => FloatingActionButton(
              onPressed: increment,
              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:


dependencies:
  redux_persist_flutter: ^0.6.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.6.0 Aug 10, 2018 Go to the documentation of redux_persist_flutter 0.6.0 Download redux_persist_flutter 0.6.0 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.8.0-rc.0 Nov 6, 2018 Go to the documentation of redux_persist_flutter 0.8.0-rc.0 Download redux_persist_flutter 0.8.0-rc.0 archive

All 11 versions...

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

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

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

The description is too short. (-20 points)

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
flutter 0.0.0
path_provider ^0.4.0 0.4.1
redux_persist ^0.7.0 0.7.0 0.8.0-rc.1
shared_preferences ^0.4.0 0.4.3
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_redux ^0.5.2
redux ^3.0.0 3.0.0