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

Question [hydrated_bloc] sophisticated example #2392

Open
II11II opened this issue Apr 15, 2021 · 8 comments
Open

Question [hydrated_bloc] sophisticated example #2392

II11II opened this issue Apr 15, 2021 · 8 comments

Comments

@II11II
Copy link

@II11II II11II commented Apr 15, 2021

Firstly, I would like to thank the creator @felangel and contributors to these packages.

I would like to ask to add one sophisticated example usage of hydrated bloc according to the real problem( Manage to save categories to local database using hydrated bloc)

As known, nowadays numerous applications demands complicated feature (such as saving categories and its subcategories locally). Adding this kind of example will help developers to use hydrated bloc correctly.

Real-world API example:
"some_url/category/getRoot" will return this kind of response
"some_url/category/${id}" will return subcategories of that catgories


[
  {
        "name_of_category":"Some categories"
        "iconHashId": "ZjEv2eRa1",
        "id": 46
    },  
    {
        "name_of_category":"Some categories"
        "iconHashId": "ZjEv2eRa1",
        "id": 67
    },

]

After getting these details, it should be saved locally using a hydrated bloc.

It would be nice if you add example which shows how to properly use Hydrated bloc

@felangel felangel self-assigned this Apr 19, 2021
@felangel felangel added this to To do in bloc via automation Apr 19, 2021
@felangel
Copy link
Owner

@felangel felangel commented Apr 19, 2021

Hi @II11II 👋
Thanks for opening an issue!

The latest version of the weather app is using hydrated_bloc to cache the weather information so that it is available offline. Is there something in particular that you're having trouble achieving? I think the weather app should provide a good overview of how you could use hydrated_bloc to accomplish what you're asking for. Thanks 🙏

@II11II
Copy link
Author

@II11II II11II commented Apr 19, 2021

Hi @felangel 🖐,

Yes, there are some trouble to save nested data using hydrated bloc.

The example of dilemma i provided above.

@II11II
Copy link
Author

@II11II II11II commented Apr 20, 2021

Here example:
Feature as Developer should create using hydrated bloc
https://vimeo.com/539327490

@kovacbb
Copy link

@kovacbb kovacbb commented Apr 20, 2021

Here example:
Feature as Developer should create using hydrated bloc
https://vimeo.com/539327490

Is there a code example?

@II11II
Copy link
Author

@II11II II11II commented Apr 21, 2021

Here example:
Feature as Developer should create using hydrated bloc
https://vimeo.com/539327490

Is there a code example?

Hi @kovacbb 🖐

Actually, I could not write good code for state and bloc and wrote using streams + sqflite it. So there is no code I can provide where the hydrated bloc is used, for that, I asked for example for such kind of feature using hydrated bloc😅

@minhdanh
Copy link

@minhdanh minhdanh commented Jul 28, 2021

@felangel What if I have multiple states (that extends from a parent state) with different properties. Can I use hydrated_bloc in that case, or I have to stick to the parent state and wrap all the properties inside that?

@felangel
Copy link
Owner

@felangel felangel commented Jul 28, 2021

@felangel What if I have multiple states (that extends from a parent state) with different properties. Can I use hydrated_bloc in that case, or I have to stick to the parent state and wrap all the properties inside that?

You can still use HydratedBloc in that case. You just need to adjust the toJson and fromJson implementations to check the type of the state and perform the correct (de)serialization.

@evanholt1
Copy link

@evanholt1 evanholt1 commented Sep 6, 2021

In case @II11II or anyone else may be helped by this:
The way i started to prefer using hydrated_bloc for complex use cases & state classes is by:

  1. storing the state class name state.runtimeType.toString() in the toJson() method of each child state class.
  2. making a factory public fromJson() in the parent abstract state class, which will call specific child constructors based on the string state value stored in step 1.
  3. if there are any variables defined in the abstract parent class, i also make a private fromJson() in the parent abstract class, which will set it's values , and will be called with :super._fromJson() in the child state classes.

this way i can use one-liners for fromJson() and toJson() in the cubit class,
deferring the specifics to the state classes, can define variables in abstract parent state classes,
and also can not repeat code and variables in child constructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
bloc
  
To do
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants