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

5,257

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
5,257
Jithin
Author by

Jithin

I just code for fun all the time... P.S. Even at work (with professional touch)...because it's super cool to code...

Updated on December 13, 2022

Comments

  • Jithin
    Jithin over 1 year

    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?

  • Jithin
    Jithin over 4 years
    but is there any app that doesn't redraw occasionally? If that's the case then most of the widgets will be stateful right?
  • Durdu
    Durdu over 4 years
    Ofcourse they redraw but the point is to make the apps as smooth as possible and one way you can do this is by having less to build when calling setState for example. Maybe on some "lightweight" screen running on a "powerful" device all will seem fine, but when this changes you might experience some UI that is not that nice ( slow/jerky/unresponsive )
  • Jithin
    Jithin over 4 years
    so let't assume that over time stateful widgets are optimized, then we could only use stateful widgets than stateless right?
  • Durdu
    Durdu over 4 years
    Even so why would you use an overhead (the state object) on a widget when you don't need it or use it? Yes, you could use a Stateful instead of a Stateless every time but that is breaking KISS or any other clean code guideline. You will have stuff you will never use, which is usually a bad sign.