Understanding state and stateful widgets

The widgets that we've seen so far are stateless widgets, meaning that once created they are immutable, and they do not keep any state information. When you interact with your users, you expect things to change. For example, if you want to convert a measure from one system to another, the result must change, based on some user input.

The most basic way to deal with changes in Flutter is using State.

State is information that can be used when a widget is built and can change during the lifetime of a widget.

An important part of this definition is that state is information that can change, and the most obvious takeaway of this concept is that when you want to add interactivity to your app, you can use State. But, if you read this definition thoroughly, it also means that it's not the widget itself that will change, it's the State of a widget that will change, and when it does, the widget will be rebuilt. When a widget has a State, it's called a stateful widget. And in Flutter, stateful widgets are immutable. It's only the State itself that changes.


Each time the State changes, the widget gets rebuilt.

Let's have a look at the main differences between a stateless widget, which we've used so far, and a stateful widget. Of course, the most obvious difference is explained by the name itself, the State: State/less and State/ful.

But there is a different implementation as well. In order to see it in detail, we'll create a new app and see it in practice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150