Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Hydrated blocs with different storages in the same app #1421

Open
felangel opened this issue Jul 9, 2020 · 4 comments
Open
Assignees
Labels
enhancement New feature or request pkg:hydrated_bloc This issue is related to the hydrated_bloc package
Milestone

Comments

@felangel
Copy link
Owner

felangel commented Jul 9, 2020

From @ncuillery (original issue)

Is your feature request related to a problem? Please describe.
I have some hydrated blocs that use the default temp folder (as a cache for my information coming the network: perform the fetching request only if the data is older than 12 hours).
But I also would like to use a hydrated bloc using the shared preferences storage to store my user preferences such as language, newsletter optin, etc.
I can't do that because, if I implement a custom storage in a BlocDelegate, all my blocs will use this storage.

Describe the solution you'd like
I guess one solution would be to not using BlocDelegate for implementing custom storage. Or improve the BlocSupervisor/BlocDelegate API to allow multiple BlocDelegates.

Additional context
I think I'd have the same problem if I already use a SimpleBlocDelegate for logging purpose and I want to write an other BlocDelegate for custom hydrated bloc storage.

cc @orsenkucher

@felangel felangel added enhancement candidate Candidate for enhancement but additional research is needed pkg:hydrated_bloc This issue is related to the hydrated_bloc package labels Jul 9, 2020
@elias8
Copy link

elias8 commented Jun 27, 2021

Hello @felangel 👋

I think we can let the user provide the Storage optionally to each HydratedBloc. And we can use the HydratedBloc.storage as a fallback to all HydratedBlocs if custom storage is not provided via the constructor.

The implementation will look like this:

mixin HydratedMixin<State> on BlocBase<State> {
  late Storage _storage;

  void hydrate([Storage? storage]) {
    _storage = storage ?? HydratedBloc.storage;
    try {
      final stateJson = _toJson(state);
      if (stateJson != null) {
        _storage.write(storageToken, stateJson).then((_) {}, onError: onError);
      }
    } catch (error, stackTrace) {
      onError(error, stackTrace);
    }
  }
}
abstract class HydratedBloc<Event, State> extends Bloc<Event, State>
    with HydratedMixin {

  HydratedBloc(State state, [Storage? storage]) : super(state) {
    hydrate(storage);
  }
}

This will be non-breaking, and calling the hydrate method inside the bloc constructor will remain necessary for those who will use the mixin.

@felangel felangel added this to the v8.0.0 milestone Oct 25, 2021
@felangel felangel self-assigned this Nov 15, 2021
@felangel felangel added enhancement New feature or request and removed enhancement candidate Candidate for enhancement but additional research is needed labels Nov 15, 2021
@felangel felangel modified the milestones: v8.0.0, v8.1.0 Nov 16, 2021
@tmaihoff
Copy link
Contributor

tmaihoff commented Oct 1, 2024

Hey, is there an update?
I have just one hydrated bloc which should use shared_preferences as storage while the users will stay on the temporary diretory.

I did not manage to implement that yet as the HydratedMixin just calls HydratedBloc.storage.

@felangel
Copy link
Owner Author

felangel commented Oct 1, 2024

Hey, is there an update? I have just one hydrated bloc which should use shared_preferences as storage while the users will stay on the temporary diretory.

I did not manage to implement that yet as the HydratedMixin just calls HydratedBloc.storage.

Happy to revisit this. The scopes/zone based approach wasn’t feasible due flutter not playing nicely with zones.

@tmaihoff
Copy link
Contributor

tmaihoff commented Oct 2, 2024

Hey, is there an update? I have just one hydrated bloc which should use shared_preferences as storage while the users will stay on the temporary diretory.
I did not manage to implement that yet as the HydratedMixin just calls HydratedBloc.storage.

Happy to revisit this. The scopes/zone based approach wasn’t feasible due flutter not playing nicely with zones.

That would be great, thanks!

There was a PR 3 years ago which implemented that with quite minimal and non-breaking changes, maybe that's the way to go when scopes/zones do not work out? 🙂

Edit: Just came across this currently open PR which seems to solve the problem as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg:hydrated_bloc This issue is related to the hydrated_bloc package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants