diff --git a/packages/hydrated_bloc/lib/src/hydrated_bloc.dart b/packages/hydrated_bloc/lib/src/hydrated_bloc.dart index 386c6573dc7..977dd5fbc7a 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_bloc.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_bloc.dart @@ -33,8 +33,8 @@ import 'package:meta/meta.dart'; abstract class HydratedBloc extends Bloc with HydratedMixin { /// {@macro hydrated_bloc} - HydratedBloc(State state) : super(state) { - hydrate(); + HydratedBloc(State state, [Storage? storage]) : super(state) { + hydrate(storage); } static Storage? _storage; @@ -75,8 +75,8 @@ abstract class HydratedBloc extends Bloc abstract class HydratedCubit extends Cubit with HydratedMixin { /// {@macro hydrated_cubit} - HydratedCubit(State state) : super(state) { - hydrate(); + HydratedCubit(State state, [Storage? storage]) : super(state) { + hydrate(storage); } } @@ -116,12 +116,12 @@ mixin HydratedMixin on BlocBase { /// ... /// } /// ``` - void hydrate() { - final storage = HydratedBloc.storage; + void hydrate([Storage? storage]) { + _istorage = storage ?? HydratedBloc.storage; try { final stateJson = _toJson(state); if (stateJson != null) { - storage.write(storageToken, stateJson).then((_) {}, onError: onError); + _istorage.write(storageToken, stateJson).then((_) {}, onError: onError); } } catch (error, stackTrace) { onError(error, stackTrace); @@ -130,13 +130,13 @@ mixin HydratedMixin on BlocBase { } State? _state; + late Storage _istorage; @override State get state { - final storage = HydratedBloc.storage; if (_state != null) return _state!; try { - final stateJson = storage.read(storageToken) as Map?; + final stateJson = _istorage.read(storageToken) as Map?; if (stateJson == null) { _state = super.state; return super.state; @@ -158,12 +158,11 @@ mixin HydratedMixin on BlocBase { @override void onChange(Change change) { super.onChange(change); - final storage = HydratedBloc.storage; final state = change.nextState; try { final stateJson = _toJson(state); if (stateJson != null) { - storage.write(storageToken, stateJson).then((_) {}, onError: onError); + _istorage.write(storageToken, stateJson).then((_) {}, onError: onError); } } catch (error, stackTrace) { onError(error, stackTrace); @@ -324,7 +323,7 @@ mixin HydratedMixin on BlocBase { /// [clear] is used to wipe or invalidate the cache of a [HydratedBloc]. /// Calling [clear] will delete the cached state of the bloc /// but will not modify the current state of the bloc. - Future clear() => HydratedBloc.storage.delete(storageToken); + Future clear() => _istorage.delete(storageToken); /// Responsible for converting the `Map` representation /// of the bloc state into a concrete instance of the bloc state. @@ -354,7 +353,7 @@ class HydratedCyclicError extends HydratedUnsupportedError { /// ```dart /// void main() async { /// WidgetsFlutterBinding.ensureInitialized(); -/// HydratedCubit.storage = await HydratedStorage.build(); +/// HydratedBloc.storage = await HydratedStorage.build(); /// runApp(MyApp()); /// } /// ```