What is difference between Stateful and Stateless Flutter

Illustration by Katerina Limpitsouni

Stateless widgets are immutable. Since their properties cannot change, all values are final. On the contrary, stateful widgets maintain state.

That is the main difference. However, we can use state using Stateless widget also. And, in fact, it matters in flutter performance.

For that reason, widgets are rebuilt with the change of the state.

Does that affect flutter application?

Certainly, that rebuilds widgets and affects the performance.

Then what should we do to solve that problem?
When should I use provider Flutter?

We should use provider package to solve that problem.

Why should we use provider package?

The main reason is, provider package will rebuild only the widget that needs the value. The widget that needs the value is known as consumer.

As a result, that consumer widget only rebuilds itself. For example, consider a simple counter. As we press the button to change the state, each pressing changes the state and rebuilds the widget.

For a stateful widget, that rebuild affects the whole tree of widgets.

In Android Studio, we can watch the flutter performance.

Read More…

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Technical blog

Twitter

Published by

Sanjib Sinha

Written six books for Apress; also publish at Leanpub and Amazon to share learning-experience.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.