Difference Between Inherited Widget and BLoC? - flutter

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.

Related

Does Inherited Widget changes rebuild whole application when it's the parrent of MaterialApp?

I want to use InheritedWidget to access and change its data from anywhere in application.
I've read many articles about InheritedWidget, but I do not understand one of its behaviors.
Here it says that only widgets that are using InheritedWidget get rebuilt when data changes(which is exactly what I want).
But here it says that InheritedWidget is immutable and its data only changes when it is rebuilt itself!
So doesn't this make the whole widget tree below the InheritedWidget get rebuilt when the data changes?
How can I wrap the MaterialApp widget with InheritedWidget so I can change its data from anywhere in app and only rebuild a small Widget that is using InheritedWidget when the data changes?
I know I can implement this using provider package very easily, but in this part of application I want to use InheritedWidget if it's possible :)
An object or widget being immutable does not mean its values can't change.
You can declare a final List<widget> children, which is immutable since it is final, and still add and remove widgets from it. What is immutable is the reference.
When you use the InheritedWidget is the same. If a value changes it wont rebuild itself and all the tree below. Even it is actually changed and rebuilt, it does not necessarily rebuild all tree below, it'd happen only if the widget type or its child/children reference changed also.

How to pass data child to child widget without rebuild parent widget in flutter?

I have one ParentWidget having two child widget.
I want to pass some callback action from FirstChild() to SecondChild() Widget without rebuild ParentWidget()like setState((){}).
I want to do because ParentWidget() has many widget. And this callback action continuously happen.
And continuously setState((){}) is not viable option for ParentWidget()
For example
setState((){}) call from FirstChild() than I want to rebuild SecondChild() without rebuild ParentWidget()
StatefulWidget is only useful in very simple business logic cases. I highly recommend that you have a look at Riverpod, where you can access a "state" from any widget, regardless of who its parent widget is. And because of how Riverpod is designed, it will not trigger any unnecessary rebuilds.
Sticking with StatefulWidget, you will need to use something like InheritedWidget as a parent of both widgets, and then have them both access it.
You can get help with state management solutions like Provider or Riverpod.
I may write an example code if you use Riverpod, but generally speaking, in such case, you need to place the state outside the Parent widget into some external variable. Then you can access and share it between both Children without rebuilding Parent.

Stateful widget with no state field

I sometimes see stateful widget with the state which doesn't have any state fields. What is the reason of using such stateful widget? Is not better in that case to convert that stateful widget into stateless widget?
State objects (created by StatefulWidget) have their own lifecycle. They can override initState, didChangeDependencies, didUpdateWidget, dispose, etc to perform some tasks at specific cases. See lifecycle description in State class docs

Flutter State Management (BloC): Stateless vs Stateful widget

So I am reading through Bloc for state management for flutter.
Since Bloc allows you to sink and stream (rebuilding a widget based on the input), then is it possible to build an app mostly with stateless widgets?
As an example, let say I make lots of single stateless class widgets, thus almost everything is compartmentalized into its own stateless widget.
With the Bloc state management, I could simply rebuild a certain stateless child widget to reflect the change.
In this approach, I don't see the need of using the stateful widget. Of course, being a total beginner in flutter, I wanted to hear if this approach has any merit to it.
Is this a good approach? Any info will be much appreciated.
You're right that you can use only StatelessWidgets. You just need to be cognizant of where you create your bloc. Some ways of instantiation are more easily testable than others, like passing the bloc to your StatelessWidget as an argument.
But for implementation, I like the flutter_bloc library the best:
https://pub.dev/packages/flutter_bloc
It includes BlocProvider which automatically handles creation and disposal of blocs.
One other thing to note is that you'll often have to kick off an event in a bloc to perform some action and a StatefulWidget could be useful to run that in the initState method.
You could either say in a StatefulWidget:
initState(){
_myBloc = SomeBloc()..add(SomeEvent());
}
// Then somewhere in your widget tree
BlocProvider<MyBloc>(
create: (context) => _myBloc,
builder: (context, state) {},
)
OR, in your StatelessWidget:
BlocProvider<MyBloc>(
create: (context) => MyBloc()..add(SomeEvent()),
builder: (context, state) {},
)
You'll find what works best for you, but I've found with Flutter that it mostly depends on the situation and goal of a particular feature. There's no need to pin yourself into a habit of always needing to use a StatelessWidget, but you are right that it is possible.
You can use only Stateless Widget. But there is one problem that you should close streams before the app is disposed of. It can be handled in two ways:
First, you can use a Stateful widget and close streams of bloc in the dispose method of stateful.
Using BlocProvider. In this case, Bloc Provider is a Stateful widget only. It closes streams automatically. Then you can use bloc using BlocProvider in Stateless Widget.
But it doesn't mean that we don't need stateful widgets. Stateful widgets are important in animation for example. Animation, text input or any local changes in the widget itself shouldn't be handled in bloc or other state management. it is the duty of widget itself.

What difference between stateless and stateful widgets?

I am learning Dart/flutter and trying to understand how Widgets system works. But I can't understand what difference between stateless and stateful widgets? For example I have button. What type it have?
Let's imagine two cases.
I send text to button and it's display it.
I send text to button and it's change color.
What will be if I will create not proper Widget type?
There are 3 kind of widgets, not just 2.
Stateful widget
Stateless widget
Inherited widget
A stateless widget is like a constant. It is immutable. If you want to change what is displayed by a stateless widget, you'll have to create a new one.
Stateful widgets are the opposite. They are alive and can interact with the user. Stateful widgets have access to a method named setState, which basically says to the framework "Hello, I want to display something else. Can you redraw me please ?".
Finally, Inherited widget is a mixt of both worlds. It is immutable and stateless. But another widget (whatever it is) can subscribe to that inherited widget.
Which means that when you replace your inherited widget by a new one, all the widgets that has subscribed to the old one will be redrawn.
In the end, a stateful widget will usually be used like a Controller.
A stateless widget will be used like a View.
And the inherited widget will be your configuration file or your Model.
According to flutter.io:
Stateless widget
Stateless widgets are immutable, meaning that their properties can’t
change — all values are final.
Here is doc.
Stateful widget
Stateful widgets maintain state that might change during the lifetime
of the widget. Implementing a stateful widget requires at least two
classes: 1) a StatefulWidget class that creates an instance of 2) a
State class. The StatefulWidget class is, itself, immutable, but the
State class persists over the lifetime of the widget.
As a example if you want to change text in text widget when a button press, you have to use StatefulWidget and it will let you to change the state of a variable.
But in StatelessWidget you cannot do that because it doesn't keep state.
Read more from doc.
This tutorial will help anyone who trying to understand these two.
Check out the Flutter Interactivity Tutorial.
If your widget's build method depends entirely on its immutable constructor arguments, you should use a StatelessWidget because they're simpler. If you want to store some persistent private data that you expect to mutate over time, use a StatefulWidget and store the data on the State.