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.
Related
where should I use GetBuilder, GetX, or Obx in a flutter? I saw some answers on the internet. but can you share a simple explanation?
which one should I use when I want to read data from the firebase collection?
Simply:
use GetBuilder when you want to update the state of a widget manually from your controller, with update(),
use Obx, when you want to update a widget based on the value of an observable variable .obs, so whenever you change its value, the Obx will update automatically in your app.
use Getx when you want to update a specific Widget with an id as an example automatically, you can think of it like GetBuilder but with Obx observation
but consider using them carefully, because based on the docs, the Getbuilder consumes fewer resources so it has better performance, and Obx is based on streams so it consumes more resources.
there is more to learn about them from the official docs
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
When I use Redux in Flutter and I need to change state val, redux rebuild all app. I have some variables in some places where I want avoid rebuild app. Can I change state value without rebuild?
How can it update a list view elements without doing pull-to-refresh
If I'm adding an element in a dialog window or from an Api, how to add/update the list view elements automatically.
Should I use StreamBuilder?
Use state management pub like Getx, or something with adlistener.
Yes you can use stream builder and you can also use setstate
setState(() { });
StreamBuilder Widget builds itself based on the latest snapshot of interaction with a Stream. Thus the state of the app changes whenever new elements are added in the list as long as the connection status is active.
You can also use the combination of ListView Builder and setState method, though the state of the app needs to change whenever a new element is added. Earlier StreamBuilder widget was updating the state of the app. Various methods like adding a button to the screen, which can help the user manually change the state; can be used depending on the use case.
You can read more about it from the official docs: setState method
StreamBuilder
This answer is not exactly about updating listView, but updating a widget(PiChart). As listview is also a widget, the implementation will be the same to update without refreshing.
In this method, I am using streamBuilder to update the widget. so need not refresh the app to update the data.
https://stackoverflow.com/a/69800324/13577765
I'm Searching a lot about the Difference between Inherited Widget and Bloc State management
I found that Inherited Widget is immutable but Bloc don't
I know the mutable and Immutable concept well but I just want to ask
Why inherited widget is Immutable and what difference between it and Bloc ?
Bloc and inheritedWidget are very different things.
Bloc is component that takes events as an input, react to this event and yields state through stream. Widgets then can listen to this stream of events and rebuild when new pierce of data is available. Bloc Is independent of widgets that listen to it, or insert events.
Inherited Widget just provides piece of data down to all his children. You have access to this data through buildContext. Also you can notify inherited Widget that Its data changed, which will result in rebuilding all widgets under it.