2

Here is property as a part of the StatefulWidget class

class MyWidget extends StatefulWidget {
  String property = "";
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Here is how I would accessproperty from the State's build method: widget.property

Here is property as a part of the StatefulWidget's State class

class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  String property = "";
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Here is how I would access it from the State's build method: property

Looks to me like I can achieve the exact same things regardless of whether property is part of the StatefulWidget class or part of the State class.

Am I right or wrong, and if I am wrong, why?

Joel Castro
  • 485
  • 6
  • 20
  • I would say that's a practice, there is no point to declare a variable in the State object if it doesn't relate to any lifecycle methods of a `StatefulWidget` or doesn't even relate to the state updates! – Gwhyyy Nov 23 '22 at 19:59

0 Answers0