Is it mandatory for every event added to the bloc to generate a state (concerning the principles of the BLoC pattern)?
Example --
User clicks on the "Share on Whatsapp" button
We add the event for it
We do the logic inside the bloc in an if (event is ShareOnWhatsapp) condition
We do not generate a state back cause the UI doesn't need a rebuild
Is this okay to do with BLoC?
Would really appreciate references!
Related
Maybe I don't understand the purpose of BloC or Provider but I'm confused as to why we would ever want to use them instead of using Flutter's built-in state management using the Stateful widget. I've just finished an app and can't remember a point where I wished I needed something more than the defaults. Can anyone clear things up for me?
There are a few reasons to use a BloC or Provider rather than Flutter's built-in setState:
BloC and Provider offer a more robust way to manage state.
BloC and Provider make it easier to update state across multiple widgets.
BloC and Provider can be used to manage async data.
BloC and Provider offer a more modular way to structure your code.
There are some case that you need BLoC to make you easier to define each state or condition that happening inside the application.
We will start discussing creating app like https://www.tokopedia.com/ (inspect element and go with phone size preview). You will see that between widget section tokopedia_ss there are some loading animations, and when the data load complete the widget loading animation changed to viewable widget (as user).
in bloc, you will make stateLoading(), stateComplete(data), stateFailed(data). and in the controller or screen you can describe what will happen when bloc state is stateLoading etc...
Creating this case with setstate is more complicated and make your code messy, also setstate will make phone render all the Build() code. not like BloC builder, you can define each widget or section.
So, when there are 10 sections, using Bloc you able to make it render each state but when using standard setstate it will render all the 10 sections in one time every state changes.
More information about BloC: article_about_BloC
We have more than one event structure in flutter, but as the events change, we lose the values of the previous event. When we switch to a new event and state, how can we still reflect the data of the previous event on the page?
For example i have two blocs, Bloc A and Bloc B. Both Bloc's is calling different api's and when i open the screen or app Bloc A And Bloc B calls the api. Problem is sometimes Bloc A or Bloc B takes sometime to load so during this i display circular progress indicator. But it doesn't look good that is Bloc A builds successfully and displays the content and Bloc B still showing circular progress indicator. So how can i implement that is if both Bloc A & B successfully loads then display both content otherwise show only single circular progress indicator at the center. How can i achieve this ?
My way to go would be: Create additional getter in BlocA and BlocB, like BlocAStatus.
Then simply create a BlocListener on wheter BlocA or BlocB and display content only if Bloc itself is ready and the other Bloc either.
BlocBuilder<BlocA, BlocAState>(
builder: (context, state) {
if(state.blocBStatus == BlocStatus.ready) {
// display content
}
}
)
In order to properly communicate between BLoCs, reading Bloc-to-Bloc Communication documentation section should be helpful.
https://bloclibrary.dev/#/architecture?id=bloc-to-bloc-communication
So I have a Cubit that is listening to an audio stream. When I first run load the page, the bloc builder rebuilds all of my UI as expected.
I then navigate away from the page, and try to resume activity. I am able to call a cubit function to trigger the hardware stream. My cubit is receiving that stream of data. I am able to print out logs from the hardware stream from within my cubit listener function, but when I call emit(NewState), the bloc builder doesn't rebuild.
I have used mobx for state management. And the good thing about the mobx is that you can observe the variable and tie it to the UI so that when a variable changes the UI updates.
I have been trying out the bloc for state management. the bloc is the map between the events to the states which makes it easier to separate the UI from the logic.
But what the appropriate replacement of the observableList from the mobx in the bloc?
How to handle the updating list properly in the bloc, it seems if anything updates in the list then just pass the entire updated list again.
in mobx we have to declare the list as ObservableList and attach it to the UI and as the list gets updated the UI gets updated.