createState
:
Creates the mutable state for this widget at a given location in the tree.
https://api.flutter.dev/flutter/widgets/StatefulWidget/createState.html
Now, in code:
class A extends StatefulWidget {
@override
_AState createState() => _AState();
}
class _AState extends State<A> {
}
Here we create a separate class named _AState
which inherits from a predefined class named State
?
So, what is createState
' role here? How does it create a mutable state for us?
CodePudding user response:
In order to create a stateful widget in a flutter, it uses the createState()
method. Stateful Widget
means a widget that has a mutable state.
CodePudding user response:
With createState
you tell the StatefulWidget
which class to use as state for this widget. And you tell it here that it needs to create an instance of _AState
for this widget.
By the way, it is also recommended to write it as
State<A> createState() => _AState();
It still works the way you wrote it but the IDE might complain about
Avoid using private types in public APIs
Saying the return type is State<A>
instead of _AState
removes this warning.