In flutter there are StateFul widgets, example:
class PlanetFormAdder extends StatefulWidget {
@override
_PlanetFormAdderState createState() => _PlanetFormAdderState();
}
class _PlanetFormAdderState extends State<PlanetFormAdder> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Container();
}
}
Having that little example as a reference, where would it be better to place the attribute: final _formKey = GlobalKey<FormState>();
? Within the class that extends from StatefulWidget
or within the class that extends from State<PlanetFormAdder>
?
I ask, since I believe that the attributes of the class can be accessed PlanetFormAdder
from the class _PlanetFormAdderState
.
Thanks.
The implementation you do in the example is the correct and standard one.
The attributes of a Widget must go inside the class that extends State to be able to access them from the Widgets that you are going to render, if they were in the one that extends StatefulWidget they would not be accessible. ;)