From a6de131226b925eeb5b28aaad13022f6ba4cbb2b Mon Sep 17 00:00:00 2001 From: BambinoUA <45417992+bambinoua@users.noreply.github.com> Date: Fri, 23 Jun 2023 09:36:45 +0300 Subject: [PATCH] Inject instance storage --- .../hydrated_bloc/lib/src/hydrated_bloc.dart | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) 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()); /// } /// ```