Difference between Stateful and Stateless widgets (Dart, Flutter)

So, I was helping my friend in understanding the basic difference between Stateful and Stateless widget and as my old habit I also created note for myself I can revise them in future. I will explain it down below hope it can help someone in future looking for this. It’s a common doubt when you starting with Flutter development.

Widgets

We first have to learn about widgets to move on and understand Stateless vs Stateful widget. So widgets are basically like elements that we add in our app so our users can interact with them. They are like the building blocks in our apps which we use to create the whole user interface.

Stateless Widgets

Stateful widgets are like the static widgets and they don’t change there appearance or behaviour after you create them. They stay static and always do the same thing as they were instructor from the start. These stateless widgets have no internal memory means they don’t store any data inside them. They don’t store any past interaction with the user or changes. The best practical examples of a Stateful widgets are:

  • Images
  • Text
  • Icons
  • AppBar
    If you are trying to make or add the above widgets into your app then always use stateless widgets for them to save user’s memory.

Stateful Widget

Stateful Widget can change there appearances or behaviour based on many different factors, it can depend can many factors like user inputs, interactions or even changes in the app. Stateful widgets have their own internal memory which is known as state and this allow the widget to remember the changes. Stateful widgets can store data internally and update it as needed based on user input, external events, or changes in the application’s data. They can remember the current values of variables or properties and use that information to update their appearance or behaviour. Some practical example of Stateful widgets are:

  • Form
  • Slider
  • Video Player
  • Shopping Cart
    So if you are trying to make some widget mentioned above or resembles the function of above widgets then only use Stateful widget to save memory. When you learn more about Flutter development you will start to use different alternatives of Stateful widgets like bloc or riverpods but for beginners Stateful widgets are the way to go.
1 Like