@@ -12,6 +12,8 @@ import 'package:redux/redux.dart';
12
12
class StoreProvider <S > extends InheritedWidget {
13
13
final Store <S > _store;
14
14
15
+ /// Create a [StoreProvider] by passing in the required [store] and [child]
16
+ /// parameters.
15
17
const StoreProvider ({
16
18
Key key,
17
19
@required Store <S > store,
@@ -21,10 +23,28 @@ class StoreProvider<S> extends InheritedWidget {
21
23
_store = store,
22
24
super (key: key, child: child);
23
25
26
+ /// A method that can be called by descendant Widgets to retrieve the Store
27
+ /// from the StoreProvider.
28
+ ///
29
+ /// Important: When using this method, pass through complete type information
30
+ /// or Flutter will be unable to find the correct StoreProvider!
31
+ ///
32
+ /// ### Example
33
+ ///
34
+ /// ```
35
+ /// class MyWidget extends StatelessWidget {
36
+ /// @override
37
+ /// Widget build(BuildContext context) {
38
+ /// final store = StoreProvider.of<int>(context);
39
+ ///
40
+ /// return Text('${store.state}');
41
+ /// }
42
+ /// }
43
+ /// ```
24
44
static Store <S > of <S >(BuildContext context) {
25
45
final type = _typeOf <StoreProvider <S >>();
26
- final StoreProvider < S > provider =
27
- context.inheritFromWidgetOfExactType (type);
46
+ final provider =
47
+ context.inheritFromWidgetOfExactType (type) as StoreProvider < S > ;
28
48
29
49
if (provider == null ) throw StoreProviderError (type);
30
50
@@ -35,7 +55,8 @@ class StoreProvider<S> extends InheritedWidget {
35
55
static Type _typeOf <T >() => T ;
36
56
37
57
@override
38
- bool updateShouldNotify (StoreProvider <S > old) => _store != old._store;
58
+ bool updateShouldNotify (StoreProvider <S > oldWidget) =>
59
+ _store != oldWidget._store;
39
60
}
40
61
41
62
/// Build a Widget using the [BuildContext] and [ViewModel] . The [ViewModel] is
@@ -109,7 +130,7 @@ typedef OnDidChangeCallback<ViewModel> = void Function(ViewModel viewModel);
109
130
110
131
/// A function that will be run after the Widget is built the first time.
111
132
///
112
- /// This function is passed the initial `ViewModel` created by the [ converter]
133
+ /// This function is passed the initial `ViewModel` created by the ` converter`
113
134
/// function.
114
135
///
115
136
/// This can be useful for starting certain animations, such as showing
@@ -208,6 +229,12 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
208
229
/// Snackbars, after the Widget is built the first time.
209
230
final OnInitialBuildCallback <ViewModel > onInitialBuild;
210
231
232
+ /// Create a [StoreConnector] by passing in the required [converter] and
233
+ /// [builder] functions.
234
+ ///
235
+ /// You can also specify a number of additional parameters that allow you to
236
+ /// modify the behavior of the StoreConnector. Please see the documentation
237
+ /// for each option for more info.
211
238
StoreConnector ({
212
239
Key key,
213
240
@required this .builder,
@@ -296,6 +323,7 @@ class StoreBuilder<S> extends StatelessWidget {
296
323
/// Snackbars, after the Widget is built the first time.
297
324
final OnInitialBuildCallback <Store <S >> onInitialBuild;
298
325
326
+ /// Create's a Widget based on the Store.
299
327
StoreBuilder ({
300
328
Key key,
301
329
@required this .builder,
@@ -401,7 +429,7 @@ class _StoreStreamListenerState<S, ViewModel>
401
429
});
402
430
}
403
431
404
- Stream < S > _stream = widget.store.onChange;
432
+ var _stream = widget.store.onChange;
405
433
406
434
if (widget.ignoreChange != null ) {
407
435
_stream = _stream.where ((state) => ! widget.ignoreChange (state));
@@ -454,15 +482,22 @@ class _StoreStreamListenerState<S, ViewModel>
454
482
}
455
483
}
456
484
485
+ /// If the StoreProvider.of method fails, this error will be thrown.
486
+ ///
487
+ /// Often, when the `of` method fails, it is difficult to understand why since
488
+ /// there can be multiple causes. This error explains those causes so the user
489
+ /// can understand and fix the issue.
457
490
class StoreProviderError extends Error {
491
+ /// The type of the class the user tried to retrieve
458
492
Type type;
459
493
494
+ /// Creates a StoreProviderError
460
495
StoreProviderError (this .type);
461
496
497
+ @override
462
498
String toString () {
463
499
return '''Error: No $type found. To fix, please try:
464
500
465
- * Using Dart 2 (required) by using the --preview-dart-2 flag
466
501
* Wrapping your MaterialApp with the StoreProvider<State>,
467
502
rather than an individual Route
468
503
* Providing full type information to your Store<State>,
0 commit comments