MOCKSTACKS
EN
Questions And Answers

More Tutorials









Flutter Two Classes


If you look at the minimum code above you will see that a Stateful Widget is composed of two classes, not one. You have one class that extends StatefulWidget, another that extends State.

Class #1 – the class that extends StatefulWidget

This is a class that is used to create the State object, class #2 in its ‘createState’ method.
An instance of this class is shorter-lived than that for the State object, class #2
The data in this class cannot change (immutable).
It is final and passed in through the constructor, same as for a StatelessWidget.

This class is thrown away and replaced when the data needs to change and a new Widget is constructed object, class #2
The data in this class cannot change (immutable).
It is final and passed in through the constructor, same as for a StatelessWidget.
This class is thrown away and replaced when the data needs to change and a new Widget is constructed.

Class #2 – the class that extends State

This is the class that does most of the work.
It holds the data that can change (mutable).
It builds the UI using the ‘build’ method.
It can respond to events, like the user clicking on a button.
An instance of this class is longer-lived than that for the StatefulWidget, class #1.
The data in this class can change.

Change the data within a lambda within the ‘setState’ method and this will ensure the UI is rebuilt.
The StatefulWidget class #1 can be thrown away and replaced and this state is then attached to the replacement.
Code in this class can refer to class #1 using the ‘widget’ variable.

Creation

When you create a Stateful Widget the following happens.
1. The instance of class #1 (the class that extends StatefulWidget) is constructed.
2. The lifecycle method ‘createState’ of class #1 (the class that extends StatefulWidget) is invoked by Flutter to create the instance of class #2 (the class that extends State).
3. The instance of class #2 (the class that extends State) is constructed.
4. The method ‘build’ of the State class (created in 3) is invoked to build the UI.


Conclusion

In this page (written and validated by ) you learned about Flutter Two Classes . What's Next? If you are interested in completing Flutter tutorial, your next topic will be learning about: Flutter Basic Material Widgets.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.