CalCOFI / OceanView

App that can incentivize ocean users to report their observations
1 stars 0 forks source link

Change all the state constructor to initState() and remove redundant variables #1

Closed hundredball closed 1 year ago

hundredball commented 2 years ago

For one-time initialization of state of stateful widget, we should use initState() instead of class constructor.

The framework calls initState. Subclasses of State should override initState to perform one-time initialization that depends on the BuildContext or the widget, which are available as the context and widget properties, respectively, when the initState method is called.

Reference: State

Instead of passing variables from the stateful widget to its state, we should use widget.A to access that variable.

class widgetA extends StatefulWidget {
  final int A;
  const widgetA({Key? key, required this.A}) : super(key: key);

  @override
  _widgetAState createState() => _widgetAState();
}

class _widgetAState extends State<widgetA> {
  late int B;

  @override
  void initState() {
    super.initState();
    B = widget.A + 2;
  }

  ...
}