Skip to content

Commit 545992c

Browse files
Taym95brianegan
authored andcommitted
Add possibly to access store from initState (#161)
* feat: add the possibility to access to store from initState by using ancestorInheritedElementForWidgetOfExactType * Add test * fix: fix test * doc: add listen git documentation
1 parent 0421c51 commit 545992c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

lib/flutter_redux.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,43 @@ class StoreProvider<S> extends InheritedWidget {
4141
/// }
4242
/// }
4343
/// ```
44-
static Store<S> of<S>(BuildContext context) {
44+
45+
/// if you need to use the [Store] from an initState function
46+
/// set the [listen] option to false
47+
///
48+
/// ### Example
49+
///
50+
/// ```
51+
/// class MyWidget extends StatefulWidget {
52+
/// static GlobalKey<_MyWidgetState> captorKey = GlobalKey<_MyWidgetState>();
53+
///
54+
/// MyWidget() : super(key: captorKey);
55+
///
56+
/// _MyWidgetState createState() => _MyWidgetState();
57+
/// }
58+
///
59+
/// class _MyWidgetState extends State<MyWidget> {
60+
/// Store<String> store;
61+
///
62+
/// @override
63+
/// void initState() {
64+
/// super.initState();
65+
/// store = StoreProvider.of<String>(context, listen: false);
66+
/// }
67+
///
68+
/// @override
69+
/// Widget build(BuildContext context) {
70+
/// return Container();
71+
/// }
72+
/// }
73+
/// ```
74+
static Store<S> of<S>(BuildContext context, {bool listen = true}) {
4575
final type = _typeOf<StoreProvider<S>>();
46-
final provider =
47-
context.inheritFromWidgetOfExactType(type) as StoreProvider<S>;
76+
final provider = (listen
77+
? context.inheritFromWidgetOfExactType(type)
78+
: context
79+
.ancestorInheritedElementForWidgetOfExactType(type)
80+
?.widget) as StoreProvider<S>;
4881

4982
if (provider == null) throw StoreProviderError(type);
5083

test/flutter_redux_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ void main() {
164164
expect(numBuilds, 2);
165165
});
166166

167+
testWidgets('can access store from initState', (WidgetTester tester) async {
168+
final store = Store<String>(
169+
identityReducer,
170+
initialState: 'I',
171+
);
172+
final widget = StoreProvider<String>(
173+
store: store,
174+
child: StoreCaptorStateful(),
175+
);
176+
177+
// Build the widget with the initial state
178+
await tester.pumpWidget(widget);
179+
180+
// Check whether the store it captures is the same as the store created at the beginning
181+
expect(StoreCaptorStateful.captorKey.currentState.store, store);
182+
});
183+
167184
testWidgets('does not rebuild if rebuildOnChange is set to false',
168185
(WidgetTester tester) async {
169186
var numBuilds = 0;
@@ -665,6 +682,30 @@ class StoreCaptor<S> extends StatelessWidget {
665682
}
666683
}
667684

685+
class StoreCaptorStateful extends StatefulWidget {
686+
static GlobalKey<_StoreCaptorStatefulState> captorKey =
687+
GlobalKey<_StoreCaptorStatefulState>();
688+
689+
StoreCaptorStateful() : super(key: captorKey);
690+
691+
_StoreCaptorStatefulState createState() => _StoreCaptorStatefulState();
692+
}
693+
694+
class _StoreCaptorStatefulState extends State<StoreCaptorStateful> {
695+
Store<String> store;
696+
697+
@override
698+
void initState() {
699+
super.initState();
700+
store = StoreProvider.of<String>(context, listen: false);
701+
}
702+
703+
@override
704+
Widget build(BuildContext context) {
705+
return Container();
706+
}
707+
}
708+
668709
String identityReducer(String state, dynamic action) {
669710
return action.toString();
670711
}

0 commit comments

Comments
 (0)