Stateful vs Stateless Widget

Introduction

In this article, I will show you what is the difference between Stateful and Stateless Widget.

As you know in Flutter all the UI components are known as widgets. The widget which contains the code for a single screen of the app can be just of two types —

  1. Stateful
  2. Stateless

Let’s discuss how do they differ.

Stateless

Stateless widgets do not require mutable state, i.e., it is immutable.

In simple words, Stateless widgets cannot change their state during the runtime of the app, which means the widgets cannot be redrawn while the app is in action.

The structure of a Stateless widget looks like this:

So, let’s understand what is there in this small code snippet.

The name of this Stateless Widget is “StartScreen”, inside which we have to override the “build” method. This build method takes in a “BuildContext” as the parameter and returns a widget. That’s why you can see that the return type of the build method is a widget. And this the place where you can design the UI of this screen, which is Stateless.

In Stateless widget, The “build” method can be called only ONCE while the app is in action, which is responsible for drawing the widgets on to the device screen.

Some examples of Stateless widgets are as follows:

Now, let’s move on to the Stateful Widget.

Stateful

Stateful widgets have a mutable state, i.e., they are mutable and can be drawn multiple times within its lifetime.

They are the widgets which can change their state multiple times and can be redrawn on to the screen any number of times while the app is in action.

The structure of a Stateful widget looks like this:

The name of the widget is again “StartScreen”, but now it overrides the “createState” method, instead of the “build” method, which returns the instance of the class “_StartScreenState”.

The class “_StartScreenState” extends from State<> which takes “StartScreen” as a template input.

Now, this “_StartScreenState” overrides the “build” method and returns a widget. This is where you can define the UI of the app, which is Stateful. As it is a Stateful widget you can call the build method any number of times, which will redraw the widgets on the screen.

So, how can you call the build method?

It’s really easy, you can use the “setState” method to call the build method, which will, in turn, redraw the widgets. This is the most important method you will need to use with any Stateful widget, to really use the statefulness of the widget.

Some examples of Stateful widgets are as follows:

Conclusion

We have come to the end of this short article. I hope you all have received a basic idea of Stateful and Stateless widgets and how do they differ from each other. These concepts would be more clear if you do some projects on your own and get the feel of how the app handles the state.

TIPS: You can quickly build a Stateless Widget in VS Code or Android Studio by using the shortcut “stl”.

27 Replies to “Stateful vs Stateless Widget”

  1. I feel that is one of the such a lot important information for me.
    And i am satisfied studying your article.
    However wanna remark on few common things, The web site taste is
    great, the articles is truly excellent : D.
    Excellent process, cheers

  2. I blog often and I truly appreciate your content.

    The article has really peaked my interest. I am going to book mark your website and keep checking for new information about once per week.
    I opted in for your Feed too.

  3. you are really a excellent webmaster. The web site loading speed is incredible.
    It seems that you’re doing any unique trick.

    In addition, The contents are masterwork.
    you have done a fantastic process on this subject!

  4. Hey there, You’ve done an incredible job. I’ll definitely digg it and personally recommend
    to my friends. I am confident they will be benefited
    from this web site.

  5. you’re in reality a excellent webmaster. The site loading pace is
    incredible. It kind of feels that you’re doing any unique trick.
    In addition, The contents are masterpiece. you’ve performed a wonderful task on this topic!

  6. Fantastic beat ! I wish to apprentice whilst you amend your website, how could i subscribe for a blog site?

    The account helped me a appropriate deal. I were tiny bit familiar of this your broadcast provided brilliant transparent concept

  7. You’ve made some really good points there. I looked on the net for more information about the issue and found
    most people will go along with your views on this website.

  8. I like the valuable info you provide in your articles.
    I’ll bookmark your weblog and check again here regularly.
    I am quite sure I’ll learn plenty of new stuff right here!
    Good luck for the next!

  9. This is a good tip especially to those fresh to the blogosphere.
    Brief but very precise information… Appreciate your sharing this one.
    A must read post!

  10. Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you!
    By the way, how could we communicate?

  11. What’s Happening i am new to this, I stumbled upon this I have found It
    absolutely useful and it has helped me out loads.
    I am hoping to contribute & aid other users like its helped me.
    Good job.

  12. I’m really enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more enjoyable for me
    to come here and visit more often. Did you hire
    out a designer to create your theme? Excellent work!

  13. Hello there! This post couldn’t be written any better! Reading through this
    post reminds me of my previous room mate! He always kept talking about this.

    I will forward this page to him. Pretty sure he will have a
    good read. Thanks for sharing!

Leave a Reply

Your email address will not be published.