@@ -33,8 +33,8 @@ import 'package:meta/meta.dart';
33
33
abstract class HydratedBloc <Event , State > extends Bloc <Event , State >
34
34
with HydratedMixin {
35
35
/// {@macro hydrated_bloc}
36
- HydratedBloc (State state) : super (state) {
37
- hydrate ();
36
+ HydratedBloc (State state, [ Storage ? storage] ) : super (state) {
37
+ hydrate (storage );
38
38
}
39
39
40
40
static Storage ? _storage;
@@ -75,8 +75,8 @@ abstract class HydratedBloc<Event, State> extends Bloc<Event, State>
75
75
abstract class HydratedCubit <State > extends Cubit <State >
76
76
with HydratedMixin <State > {
77
77
/// {@macro hydrated_cubit}
78
- HydratedCubit (State state) : super (state) {
79
- hydrate ();
78
+ HydratedCubit (State state, [ Storage ? storage] ) : super (state) {
79
+ hydrate (storage );
80
80
}
81
81
}
82
82
@@ -104,6 +104,8 @@ abstract class HydratedCubit<State> extends Cubit<State>
104
104
/// * [HydratedCubit] to enable automatic state persistence/restoration with [Cubit]
105
105
///
106
106
mixin HydratedMixin <State > on BlocBase <State > {
107
+ late final Storage __storage;
108
+
107
109
/// Populates the internal state storage with the latest state.
108
110
/// This should be called when using the [HydratedMixin]
109
111
/// directly within the constructor body.
@@ -116,10 +118,10 @@ mixin HydratedMixin<State> on BlocBase<State> {
116
118
/// ...
117
119
/// }
118
120
/// ```
119
- void hydrate () {
120
- final storage = HydratedBloc .storage;
121
+ void hydrate ([ Storage ? storage] ) {
122
+ __storage = storage ?? = HydratedBloc .storage;
121
123
try {
122
- final stateJson = storage .read (storageToken) as Map <dynamic , dynamic >? ;
124
+ final stateJson = __storage .read (storageToken) as Map <dynamic , dynamic >? ;
123
125
_state = stateJson != null ? _fromJson (stateJson) : super .state;
124
126
} catch (error, stackTrace) {
125
127
onError (error, stackTrace);
@@ -129,7 +131,7 @@ mixin HydratedMixin<State> on BlocBase<State> {
129
131
try {
130
132
final stateJson = _toJson (state);
131
133
if (stateJson != null ) {
132
- storage .write (storageToken, stateJson).then ((_) {}, onError: onError);
134
+ __storage .write (storageToken, stateJson).then ((_) {}, onError: onError);
133
135
}
134
136
} catch (error, stackTrace) {
135
137
onError (error, stackTrace);
@@ -145,12 +147,11 @@ mixin HydratedMixin<State> on BlocBase<State> {
145
147
@override
146
148
void onChange (Change <State > change) {
147
149
super .onChange (change);
148
- final storage = HydratedBloc .storage;
149
150
final state = change.nextState;
150
151
try {
151
152
final stateJson = _toJson (state);
152
153
if (stateJson != null ) {
153
- storage .write (storageToken, stateJson).then ((_) {}, onError: onError);
154
+ __storage .write (storageToken, stateJson).then ((_) {}, onError: onError);
154
155
}
155
156
} catch (error, stackTrace) {
156
157
onError (error, stackTrace);
@@ -311,7 +312,7 @@ mixin HydratedMixin<State> on BlocBase<State> {
311
312
/// [clear] is used to wipe or invalidate the cache of a [HydratedBloc] .
312
313
/// Calling [clear] will delete the cached state of the bloc
313
314
/// but will not modify the current state of the bloc.
314
- Future <void > clear () => HydratedBloc .storage .delete (storageToken);
315
+ Future <void > clear () => __storage .delete (storageToken);
315
316
316
317
/// Responsible for converting the `Map<String, dynamic>` representation
317
318
/// of the bloc state into a concrete instance of the bloc state.
0 commit comments