Skip to content

Commit eb42897

Browse files
committed
Call onInit before the ViewModel is created.
This allows users to dispatch sync actions within the onInit function. Fixes #8
1 parent 09a4749 commit eb42897

File tree

7 files changed

+124
-92
lines changed

7 files changed

+124
-92
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.5
4+
5+
* Bugfix: `onInit` was not called before the initial ViewModel is constructed.
6+
37
## 0.3.4
48

59
* Fix Changelog.

analysis_options.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
analyzer:
2+
strong-mode: true

example/lib/main.dart

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ enum Actions { Increment }
77

88
// The reducer, which takes the previous count and increments it in response
99
// to an Increment action.
10-
int counterReducer(int state, action) {
10+
int counterReducer(int state, dynamic action) {
1111
if (action == Actions.Increment) {
1212
return state + 1;
1313
}
@@ -16,14 +16,23 @@ int counterReducer(int state, action) {
1616
}
1717

1818
void main() {
19-
runApp(new FlutterReduxApp());
20-
}
21-
22-
class FlutterReduxApp extends StatelessWidget {
2319
// Create your store as a final variable in a base Widget. This works better
2420
// with Hot Reload than creating it directly in the `build` function.
2521
final store = new Store<int>(counterReducer, initialState: 0);
2622

23+
runApp(new FlutterReduxApp(store: store));
24+
}
25+
26+
class FlutterReduxApp extends StatefulWidget {
27+
final Store<int> store;
28+
29+
FlutterReduxApp({Key key, this.store}) : super(key: key);
30+
31+
@override
32+
_FlutterReduxAppState createState() => new _FlutterReduxAppState();
33+
}
34+
35+
class _FlutterReduxAppState extends State<FlutterReduxApp> {
2736
@override
2837
Widget build(BuildContext context) {
2938
final title = 'Flutter Redux Demo';
@@ -34,7 +43,7 @@ class FlutterReduxApp extends StatelessWidget {
3443
home: new StoreProvider(
3544
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
3645
// Widgets will find and use this value as the `Store`.
37-
store: store,
46+
store: widget.store,
3847
child: new Scaffold(
3948
appBar: new AppBar(
4049
title: new Text(title),

example/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ dependencies:
77

88
dev_dependencies:
99
redux: ">=2.0.0 <3.0.0"
10-
flutter_redux: ">=0.3.0 <0.4.0"
10+
flutter_redux:
11+
path: ../
1112
flutter_test:
1213
sdk: flutter
1314

lib/flutter_redux.dart

+6-5
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class _StoreStreamListener<S, ViewModel> extends StatefulWidget {
228228
final bool distinct;
229229
final OnInitCallback onInit;
230230
final OnDisposeCallback onDispose;
231-
final IgnoreChangeTest ignoreChange;
231+
final IgnoreChangeTest<S> ignoreChange;
232232

233233
_StoreStreamListener({
234234
Key key,
@@ -255,7 +255,12 @@ class _StoreStreamListenerState<ViewModel> extends State<_StoreStreamListener> {
255255

256256
@override
257257
void initState() {
258+
if (widget.onInit != null) {
259+
widget.onInit(widget.store);
260+
}
261+
258262
_init();
263+
259264
super.initState();
260265
}
261266

@@ -306,10 +311,6 @@ class _StoreStreamListenerState<ViewModel> extends State<_StoreStreamListener> {
306311
latestValue = vm;
307312
sink.add(vm);
308313
}));
309-
310-
if (widget.onInit != null) {
311-
widget.onInit(widget.store);
312-
}
313314
}
314315

315316
@override

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_redux
22
description: A library that connects Widgets to a Redux Store
3-
version: 0.3.4
3+
version: 0.3.5
44
author: Brian Egan <[email protected]>
55
homepage: https://github.com/brianegan/flutter_redux
66

0 commit comments

Comments
 (0)