Flutter Storage Engine for redux_persist
.
Can either save to shared_preferences
(default, recommended), or your
application document directory.
Defaults to saving to
shared_preferences
(recommended).
To save to a file in your
application document directory,
simply change the location
:
var persistor = new Persistor<AppState>(
// ...
storage: new FlutterStorage("my-app"),
);
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)
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(),
);
Please file feature requests and bugs at the issue tracker.
child
to builder
.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"]);
}
Map toJson() => {'counter': counter};
}
class IncrementCounterAction {}
AppState reducer(state, action) {
// Load to state
if (action is LoadAction<AppState>) {
return action.state ?? state;
}
switch (action.runtimeType) {
case IncrementCounterAction:
return state.copyWith(counter: state.counter + 1);
default:
// No change
return state;
}
}
// App
class MyApp extends StatelessWidget {
var persistor;
var 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.load(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),
),
),
);
}
}
Add this to your package's pubspec.yaml file:
dependencies:
redux_persist_flutter: ^0.4.2
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:redux_persist_flutter/redux_persist_flutter.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.8.1 | Feb 11, 2019 |
|
|
0.8.0 | Nov 27, 2018 |
|
|
0.6.0 | Aug 10, 2018 |
|
|
0.5.0 | Mar 19, 2018 |
|
|
0.4.6 | Mar 14, 2018 |
|
|
0.4.5 | Mar 13, 2018 |
|
|
0.4.4 | Mar 12, 2018 |
|
|
0.4.3 | Mar 12, 2018 |
|
|
0.4.2 | Mar 10, 2018 |
|
|
0.4.1 | Mar 10, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
94
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
47
|
The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.
Support Dart 2 in pubspec.yaml
.
The SDK constraint in pubspec.yaml
doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.
Make sure dartdoc
successfully runs on your package's source files. (-10 points)
Running dartdoc
failed with the following output:
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.24.0 <2.0.0 |