Why are stateful widgets defined as two classes in flutter?

后端 未结 3 572
轮回少年
轮回少年 2020-12-24 06:17

I\'m new to flutter/dart, so while I try to make an app I also try to understand why things are a certain way. In the flutter docs there is example code of a stateful widget

3条回答
  •  一整个雨季
    2020-12-24 06:36

    class YellowBird extends StatefulWidget

    StatefulWidget is an immutable class (Immutable class means that once an object is created, we cannot change its values). 1- The class must be declared as final (So that child classes can’t be created) 2- Data members in the class must be declared as final (So that we can’t change the value of it after object creation) 3- A parameterized constructor 4- Getter method for all the variables in it. No setters(To not have the option to change the value of the instance variable)

    class _YellowBirdState extends State

    State class which type is generic is a mutable class that can be instantiated with different values after creating its object.


    same as this StatefullWdget class which is immutable is calling a function of createState() which define the class State of the widget after its called in a flutter so we can change the values of widget again and again by this approach but we cannot change the type of Stateful or Stateless.

提交回复
热议问题