Why do we need stateless widget in flutter? - flutter

As the below diagram shows Stateful widget covers what Stateless widget do, When I can do the same thing with Stateful widget, why Flutter designers added another widget? For increasing performance?
Please do not post the difference just answer the Why?

Would you always use a sword to do a knife's job? Given that you always have both of them available with easy access. The knife can't do everything a sword can, but would that mean that a sword should be used by default for everything, since it's more robust?
You'll have many widgets where the state of the elements will not change, and others where you will not have to use setState to update your UI either(given that you would rather not use higher level state management solutions).
Using stateless widgets as long as they can do the job, means more performent widget trees.
Please do not post the difference just answer the Why?
Knowing the difference between them is key to understanding 'why', and the differences are not just in the name less vs full. But sensing that you aren't interested, that's why.

Good Flutter code seperates the code into many small Widgets. In addition to better performance creating a StatefulWidget means that you have to write more code and add unnecessary complexity to your code.

For better performance. Stateless widgets are usually only called in one of these three situations:
1- widget is inserted in the tree.
2- Widget's parent changes its configuration
3- Inherited Widget it depends on changes.

Related

how to exclude a widget from setState?

I am designing an application that uses setState to refresh a screen with all its widgets, however I would like to know if there is a way to exclude a particular widget (conainer) and all its children, as if I were treating it as a new page.
I tried to use Navigator.push (context, MaterialPageRoute), but it didn't work, I would like to treat the widget as a separate page. I appreciate any help
Short answer: You can't.
Long answer: You can change where the setState is called, create a separate StatefulWidget that contains only the ones that need to be rebuilded. However in most of the cases this is not possible because of the order you need to show your widgets or many other factors. That's when State Managements tools come in handy. You should try to learn how to use Provider, Riverpod or BLoC (which i personally recommend). If you want something a litte bit easier you can start learning how to use InheritedWidget starting in the documentation right here.
use const if possible. if not,
use StatefulBuilder , if you call setState inside this widget, the outside widget is not touched. Maybe it will suit what you need

Is there a replacement for initState and didChangeDependencies methods for Stateless Widgets?

I don't have any code to show just a question which makes me keep thinking of a better approach, every time I need to load data before rendering it to the image. I use initState or didChangeDependencies methods for that, but to keep that I've to make the whole widget stateful to access these methods.
So my question is there a better approach for Stateless Widgets not to be converted to Stateful and still get the same results are same.
I tried finding some results but couldn't, sorry if the issue already exists!
Thanks!
You can use state-management approaches flutter had provided like Provider, Bloc, Riverpod and many others. Choose one of them and you can manage the state of the widget using stateless widget.

Why do we need stateless widgets if the same can be achieved by stateful widgets in flutter?

I am a newbie in the world of flutter, and I recently learned (or I think I did) about stateful and stateless widgets which is kind of the base for flutter widgets.
We use stateless widgets for things that are not redrawn on the display, (like text, button etc.) but stateful widgets can redraw themselves.
So my question is why do we need stateless widgets if stateful widgets can be used to draw the same kind of widgets that a stateless widget can?
Or is there any specific reasons to use stateless over stateful widgets in flutter? Or can we use stateful widgets all the time rather than stateless widgets which can draw content only once?
Thanks, and sorry if this is a stupid question.
EDIT
Well the question is not the difference between stateless and stateful.
I know the difference but what is the impact of using only stateful widgets since by using it we could also implement most of the things a stateless widget can do then why do we need stateless widgets?what's the importance of it in a flutter environment were most of the apps will be re-drawn time-to-time?
From their documentation:
Stateless widget are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the widget is inflated. (= use when you don't need to "update the UI here").
Stateful widgets are more resource consuming and you always need to think about performance.
Here is more about this.
Push the state to the leaves. For example, if your page has a ticking
clock, rather than putting the state at the top of the page and
rebuilding the entire page each time the clock ticks, create a
dedicated clock widget that only updates itself.
Even more on this :)
I hope this answers your question.
Yes, StatefulWidget can rebuild. That happens typically when using Inheritedwidgets.
StatelessWidget exists to split a big widget tree into smaller reusable widgets.
You might think "but I can use StatefulWidget or functions for this". Which is true, but not exactly:
StatefulWidget comes with a huge boilerplate, which you do not need in that situation. So this just adds noise and makes your code less readable.
Functions cannot rebuild independently, nor to they have access to key and override ==. So they can be less performant or introduce bugs.
Every time we use a Stateful widget, a state object is created. If we use all Stateful widgets, there will be a lot of unnecessary state objects which will consume a lot of memory. So where we don't need to change the state, we should use the simpler one - the Stateless widget.
Another reason to use Stateless widgets over Stateful widgets is Stateful widget comes with a huge boilerplate and according to Flutter API Documentation using a bunch of nested Stateful widgets, passing data through all those build methods and constructors can get cumbersome.
This is what I understand ...
When you use a stateful widget and redraw it, all other widgets inside will be redrawn as well. So we try to use stateless widgets so as not to redraw the other widgets within them, but you know we generally need to change the data on the screen and it should happen inside a "single widget" and this widget should be the one stateful widget to use as little computing power as possible.
Now ... I guess you're thinking: "but what if i just use stateful widgets and don't redraw them?" well, as you know, when you use a stateful widget you have two classes: widget and state. I have gotten that when you declare a state you tell the phone to keep and save this state in memory whether the widget is redrawn or not, so you use the phone memory for no reason because you don't need to redraw its widget.
We should think about always using stateless widgets because they are lighter than stateful widgets and we should always make our stateful widgets the smallest in our application to redraw as few widgets as possible in the widget tree of the application.
I hope I helped a little.
Theoretically I think you can use stateful widgets every where, but your overall program would be heavier and would have a lot of redundancies.
That said, I don't think there is any advantage of using stateless widgets over stateful widgets.

Should screens be Stateless or Stateful?

I'm developing an app with lots of screens and pages. I've read somewhere that you should use Stateless Widgets whenever you can.
Why is that?
If I have a lot of screens, should those be stateless? And then the content inside be Stateful? Is it better to have both the screen and widgets inside being Stateful?
You should ask yourself some questions about the screen/page to decide if it will be Stateless or Stateful.
The most obvious, does it need to change state?
Do you need to call initState, didChangeDependencies or another lifecycle method?
Is a bad practice to make Stateful when not needed. A good idea might be to start always as Stateless widget and if needed you can change it to Stateful easily with Alt + Enter shortcut (Android Studio).
I always start by creating a Stateless Widget and work with it until I have to change something state. So I could quickly use `Alt-Enter/ Convert to a Statefull from Intellij/AS to change it to stateful. (doing the inverse is not so easy, so...).
Moreover, if you use Stateful widget with some async mechanism, like streams, you could build the widget once and use the streams to update the information you need, and it will not impact the performance of your app so much. But if you call setState many times, this can degrade your App, since for every setState the Widget tree will be rebuilt.
This article from the flutter docs shows interesting tips about handling state change in flutter apps:

Inter Widget communication

Is it possible to do inter widget communication via something like a notification/event bus?
I need to be able to tell one widget to react to something that happened in another and didn't want to create a hard link.
The notification listener will only fire is if it is higher in the widget tree than both of the widgets so that isn't probably a viable solution.
There are lots of ways to do this depending on your use case.
You could have them be AnimatedWidgets that are passed a ValueNotifier or ChangeNotifier as the listenable. You can see this pattern in the Gallery's animation example.
You could use StreamBuilder to have your widgets rebuild automatically when new events come in on a Stream. There aren't a lot of examples of this in the main Flutter repo, but it's something that you're likely to need once you start using plugins or doing network I/O.
You could use a GlobalKey to get currentState and have one State call methods on the other. This is how snackbars work (example).
You can also extend InheritedWidget to provide widgets with information that wasn't passed as a constructor argument, and they'll automatically be marked for rebuild when that information changes. This is how Themes work, for example.
If you can provide more details on what your widgets do / what their relationship is, or ideally a code snippet I can help you decide which approach would make the most sense for your situation.